MAINTAINERS: fix style in KEYS-TRUSTED entry
[linux-2.6-microblaze.git] / drivers / regulator / act8865-regulator.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * act8865-regulator.c - Voltage regulation for active-semi ACT88xx PMUs
4  *
5  * http://www.active-semi.com/products/power-management-units/act88xx/
6  *
7  * Copyright (C) 2013 Atmel Corporation
8  */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
13 #include <linux/err.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/act8865.h>
17 #include <linux/of.h>
18 #include <linux/of_device.h>
19 #include <linux/regulator/of_regulator.h>
20 #include <linux/regmap.h>
21
22 /*
23  * ACT8600 Global Register Map.
24  */
25 #define ACT8600_SYS_MODE        0x00
26 #define ACT8600_SYS_CTRL        0x01
27 #define ACT8600_DCDC1_VSET      0x10
28 #define ACT8600_DCDC1_CTRL      0x12
29 #define ACT8600_DCDC2_VSET      0x20
30 #define ACT8600_DCDC2_CTRL      0x22
31 #define ACT8600_DCDC3_VSET      0x30
32 #define ACT8600_DCDC3_CTRL      0x32
33 #define ACT8600_SUDCDC4_VSET    0x40
34 #define ACT8600_SUDCDC4_CTRL    0x41
35 #define ACT8600_LDO5_VSET       0x50
36 #define ACT8600_LDO5_CTRL       0x51
37 #define ACT8600_LDO6_VSET       0x60
38 #define ACT8600_LDO6_CTRL       0x61
39 #define ACT8600_LDO7_VSET       0x70
40 #define ACT8600_LDO7_CTRL       0x71
41 #define ACT8600_LDO8_VSET       0x80
42 #define ACT8600_LDO8_CTRL       0x81
43 #define ACT8600_LDO910_CTRL     0x91
44 #define ACT8600_APCH0           0xA1
45 #define ACT8600_APCH1           0xA8
46 #define ACT8600_APCH2           0xA9
47 #define ACT8600_APCH_STAT       0xAA
48 #define ACT8600_OTG0            0xB0
49 #define ACT8600_OTG1            0xB2
50
51 /*
52  * ACT8846 Global Register Map.
53  */
54 #define ACT8846_SYS0            0x00
55 #define ACT8846_SYS1            0x01
56 #define ACT8846_REG1_VSET       0x10
57 #define ACT8846_REG1_CTRL       0x12
58 #define ACT8846_REG2_VSET0      0x20
59 #define ACT8846_REG2_VSET1      0x21
60 #define ACT8846_REG2_CTRL       0x22
61 #define ACT8846_REG3_VSET0      0x30
62 #define ACT8846_REG3_VSET1      0x31
63 #define ACT8846_REG3_CTRL       0x32
64 #define ACT8846_REG4_VSET0      0x40
65 #define ACT8846_REG4_VSET1      0x41
66 #define ACT8846_REG4_CTRL       0x42
67 #define ACT8846_REG5_VSET       0x50
68 #define ACT8846_REG5_CTRL       0x51
69 #define ACT8846_REG6_VSET       0x58
70 #define ACT8846_REG6_CTRL       0x59
71 #define ACT8846_REG7_VSET       0x60
72 #define ACT8846_REG7_CTRL       0x61
73 #define ACT8846_REG8_VSET       0x68
74 #define ACT8846_REG8_CTRL       0x69
75 #define ACT8846_REG9_VSET       0x70
76 #define ACT8846_REG9_CTRL       0x71
77 #define ACT8846_REG10_VSET      0x80
78 #define ACT8846_REG10_CTRL      0x81
79 #define ACT8846_REG11_VSET      0x90
80 #define ACT8846_REG11_CTRL      0x91
81 #define ACT8846_REG12_VSET      0xa0
82 #define ACT8846_REG12_CTRL      0xa1
83 #define ACT8846_REG13_CTRL      0xb1
84 #define ACT8846_GLB_OFF_CTRL    0xc3
85 #define ACT8846_OFF_SYSMASK     0x18
86
87 /*
88  * ACT8865 Global Register Map.
89  */
90 #define ACT8865_SYS_MODE        0x00
91 #define ACT8865_SYS_CTRL        0x01
92 #define ACT8865_DCDC1_VSET1     0x20
93 #define ACT8865_DCDC1_VSET2     0x21
94 #define ACT8865_DCDC1_CTRL      0x22
95 #define ACT8865_DCDC2_VSET1     0x30
96 #define ACT8865_DCDC2_VSET2     0x31
97 #define ACT8865_DCDC2_CTRL      0x32
98 #define ACT8865_DCDC3_VSET1     0x40
99 #define ACT8865_DCDC3_VSET2     0x41
100 #define ACT8865_DCDC3_CTRL      0x42
101 #define ACT8865_LDO1_VSET       0x50
102 #define ACT8865_LDO1_CTRL       0x51
103 #define ACT8865_LDO2_VSET       0x54
104 #define ACT8865_LDO2_CTRL       0x55
105 #define ACT8865_LDO3_VSET       0x60
106 #define ACT8865_LDO3_CTRL       0x61
107 #define ACT8865_LDO4_VSET       0x64
108 #define ACT8865_LDO4_CTRL       0x65
109 #define ACT8865_MSTROFF         0x20
110
111 /*
112  * Field Definitions.
113  */
114 #define ACT8865_ENA             0x80    /* ON - [7] */
115 #define ACT8865_VSEL_MASK       0x3F    /* VSET - [5:0] */
116
117
118 #define ACT8600_LDO10_ENA               0x40    /* ON - [6] */
119 #define ACT8600_SUDCDC_VSEL_MASK        0xFF    /* SUDCDC VSET - [7:0] */
120
121 /*
122  * ACT8865 voltage number
123  */
124 #define ACT8865_VOLTAGE_NUM     64
125 #define ACT8600_SUDCDC_VOLTAGE_NUM      256
126
127 struct act8865 {
128         struct regmap *regmap;
129         int off_reg;
130         int off_mask;
131 };
132
133 static const struct regmap_range act8600_reg_ranges[] = {
134         regmap_reg_range(0x00, 0x01),
135         regmap_reg_range(0x10, 0x10),
136         regmap_reg_range(0x12, 0x12),
137         regmap_reg_range(0x20, 0x20),
138         regmap_reg_range(0x22, 0x22),
139         regmap_reg_range(0x30, 0x30),
140         regmap_reg_range(0x32, 0x32),
141         regmap_reg_range(0x40, 0x41),
142         regmap_reg_range(0x50, 0x51),
143         regmap_reg_range(0x60, 0x61),
144         regmap_reg_range(0x70, 0x71),
145         regmap_reg_range(0x80, 0x81),
146         regmap_reg_range(0x91, 0x91),
147         regmap_reg_range(0xA1, 0xA1),
148         regmap_reg_range(0xA8, 0xAA),
149         regmap_reg_range(0xB0, 0xB0),
150         regmap_reg_range(0xB2, 0xB2),
151         regmap_reg_range(0xC1, 0xC1),
152 };
153
154 static const struct regmap_range act8600_reg_ro_ranges[] = {
155         regmap_reg_range(0xAA, 0xAA),
156         regmap_reg_range(0xC1, 0xC1),
157 };
158
159 static const struct regmap_range act8600_reg_volatile_ranges[] = {
160         regmap_reg_range(0x00, 0x01),
161         regmap_reg_range(0x12, 0x12),
162         regmap_reg_range(0x22, 0x22),
163         regmap_reg_range(0x32, 0x32),
164         regmap_reg_range(0x41, 0x41),
165         regmap_reg_range(0x51, 0x51),
166         regmap_reg_range(0x61, 0x61),
167         regmap_reg_range(0x71, 0x71),
168         regmap_reg_range(0x81, 0x81),
169         regmap_reg_range(0xA8, 0xA8),
170         regmap_reg_range(0xAA, 0xAA),
171         regmap_reg_range(0xB0, 0xB0),
172         regmap_reg_range(0xC1, 0xC1),
173 };
174
175 static const struct regmap_access_table act8600_write_ranges_table = {
176         .yes_ranges     = act8600_reg_ranges,
177         .n_yes_ranges   = ARRAY_SIZE(act8600_reg_ranges),
178         .no_ranges      = act8600_reg_ro_ranges,
179         .n_no_ranges    = ARRAY_SIZE(act8600_reg_ro_ranges),
180 };
181
182 static const struct regmap_access_table act8600_read_ranges_table = {
183         .yes_ranges     = act8600_reg_ranges,
184         .n_yes_ranges   = ARRAY_SIZE(act8600_reg_ranges),
185 };
186
187 static const struct regmap_access_table act8600_volatile_ranges_table = {
188         .yes_ranges     = act8600_reg_volatile_ranges,
189         .n_yes_ranges   = ARRAY_SIZE(act8600_reg_volatile_ranges),
190 };
191
192 static const struct regmap_config act8600_regmap_config = {
193         .reg_bits = 8,
194         .val_bits = 8,
195         .max_register = 0xFF,
196         .wr_table = &act8600_write_ranges_table,
197         .rd_table = &act8600_read_ranges_table,
198         .volatile_table = &act8600_volatile_ranges_table,
199 };
200
201 static const struct regmap_config act8865_regmap_config = {
202         .reg_bits = 8,
203         .val_bits = 8,
204 };
205
206 static const struct regulator_linear_range act8865_voltage_ranges[] = {
207         REGULATOR_LINEAR_RANGE(600000, 0, 23, 25000),
208         REGULATOR_LINEAR_RANGE(1200000, 24, 47, 50000),
209         REGULATOR_LINEAR_RANGE(2400000, 48, 63, 100000),
210 };
211
212 static const struct regulator_linear_range act8600_sudcdc_voltage_ranges[] = {
213         REGULATOR_LINEAR_RANGE(3000000, 0, 63, 0),
214         REGULATOR_LINEAR_RANGE(3000000, 64, 159, 100000),
215         REGULATOR_LINEAR_RANGE(12600000, 160, 191, 200000),
216         REGULATOR_LINEAR_RANGE(19000000, 192, 247, 400000),
217         REGULATOR_LINEAR_RANGE(41400000, 248, 255, 0),
218 };
219
220 static const struct regulator_ops act8865_ops = {
221         .list_voltage           = regulator_list_voltage_linear_range,
222         .map_voltage            = regulator_map_voltage_linear_range,
223         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
224         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
225         .enable                 = regulator_enable_regmap,
226         .disable                = regulator_disable_regmap,
227         .is_enabled             = regulator_is_enabled_regmap,
228 };
229
230 static const struct regulator_ops act8865_ldo_ops = {
231         .enable                 = regulator_enable_regmap,
232         .disable                = regulator_disable_regmap,
233         .is_enabled             = regulator_is_enabled_regmap,
234 };
235
236 #define ACT88xx_REG(_name, _family, _id, _vsel_reg, _supply)            \
237         [_family##_ID_##_id] = {                                        \
238                 .name                   = _name,                        \
239                 .of_match               = of_match_ptr(_name),          \
240                 .regulators_node        = of_match_ptr("regulators"),   \
241                 .supply_name            = _supply,                      \
242                 .id                     = _family##_ID_##_id,           \
243                 .type                   = REGULATOR_VOLTAGE,            \
244                 .ops                    = &act8865_ops,                 \
245                 .n_voltages             = ACT8865_VOLTAGE_NUM,          \
246                 .linear_ranges          = act8865_voltage_ranges,       \
247                 .n_linear_ranges        = ARRAY_SIZE(act8865_voltage_ranges), \
248                 .vsel_reg               = _family##_##_id##_##_vsel_reg, \
249                 .vsel_mask              = ACT8865_VSEL_MASK,            \
250                 .enable_reg             = _family##_##_id##_CTRL,       \
251                 .enable_mask            = ACT8865_ENA,                  \
252                 .owner                  = THIS_MODULE,                  \
253         }
254
255 static const struct regulator_desc act8600_regulators[] = {
256         ACT88xx_REG("DCDC1", ACT8600, DCDC1, VSET, "vp1"),
257         ACT88xx_REG("DCDC2", ACT8600, DCDC2, VSET, "vp2"),
258         ACT88xx_REG("DCDC3", ACT8600, DCDC3, VSET, "vp3"),
259         {
260                 .name = "SUDCDC_REG4",
261                 .of_match = of_match_ptr("SUDCDC_REG4"),
262                 .regulators_node = of_match_ptr("regulators"),
263                 .id = ACT8600_ID_SUDCDC4,
264                 .ops = &act8865_ops,
265                 .type = REGULATOR_VOLTAGE,
266                 .n_voltages = ACT8600_SUDCDC_VOLTAGE_NUM,
267                 .linear_ranges = act8600_sudcdc_voltage_ranges,
268                 .n_linear_ranges = ARRAY_SIZE(act8600_sudcdc_voltage_ranges),
269                 .vsel_reg = ACT8600_SUDCDC4_VSET,
270                 .vsel_mask = ACT8600_SUDCDC_VSEL_MASK,
271                 .enable_reg = ACT8600_SUDCDC4_CTRL,
272                 .enable_mask = ACT8865_ENA,
273                 .owner = THIS_MODULE,
274         },
275         ACT88xx_REG("LDO5", ACT8600, LDO5, VSET, "inl"),
276         ACT88xx_REG("LDO6", ACT8600, LDO6, VSET, "inl"),
277         ACT88xx_REG("LDO7", ACT8600, LDO7, VSET, "inl"),
278         ACT88xx_REG("LDO8", ACT8600, LDO8, VSET, "inl"),
279         {
280                 .name = "LDO_REG9",
281                 .of_match = of_match_ptr("LDO_REG9"),
282                 .regulators_node = of_match_ptr("regulators"),
283                 .id = ACT8600_ID_LDO9,
284                 .ops = &act8865_ldo_ops,
285                 .type = REGULATOR_VOLTAGE,
286                 .n_voltages = 1,
287                 .fixed_uV = 3300000,
288                 .enable_reg = ACT8600_LDO910_CTRL,
289                 .enable_mask = ACT8865_ENA,
290                 .owner = THIS_MODULE,
291         },
292         {
293                 .name = "LDO_REG10",
294                 .of_match = of_match_ptr("LDO_REG10"),
295                 .regulators_node = of_match_ptr("regulators"),
296                 .id = ACT8600_ID_LDO10,
297                 .ops = &act8865_ldo_ops,
298                 .type = REGULATOR_VOLTAGE,
299                 .n_voltages = 1,
300                 .fixed_uV = 1200000,
301                 .enable_reg = ACT8600_LDO910_CTRL,
302                 .enable_mask = ACT8600_LDO10_ENA,
303                 .owner = THIS_MODULE,
304         },
305 };
306
307 static const struct regulator_desc act8846_regulators[] = {
308         ACT88xx_REG("REG1", ACT8846, REG1, VSET, "vp1"),
309         ACT88xx_REG("REG2", ACT8846, REG2, VSET0, "vp2"),
310         ACT88xx_REG("REG3", ACT8846, REG3, VSET0, "vp3"),
311         ACT88xx_REG("REG4", ACT8846, REG4, VSET0, "vp4"),
312         ACT88xx_REG("REG5", ACT8846, REG5, VSET, "inl1"),
313         ACT88xx_REG("REG6", ACT8846, REG6, VSET, "inl1"),
314         ACT88xx_REG("REG7", ACT8846, REG7, VSET, "inl1"),
315         ACT88xx_REG("REG8", ACT8846, REG8, VSET, "inl2"),
316         ACT88xx_REG("REG9", ACT8846, REG9, VSET, "inl2"),
317         ACT88xx_REG("REG10", ACT8846, REG10, VSET, "inl3"),
318         ACT88xx_REG("REG11", ACT8846, REG11, VSET, "inl3"),
319         ACT88xx_REG("REG12", ACT8846, REG12, VSET, "inl3"),
320 };
321
322 static const struct regulator_desc act8865_regulators[] = {
323         ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET1, "vp1"),
324         ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET1, "vp2"),
325         ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET1, "vp3"),
326         ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
327         ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
328         ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
329         ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
330 };
331
332 static const struct regulator_desc act8865_alt_regulators[] = {
333         ACT88xx_REG("DCDC_REG1", ACT8865, DCDC1, VSET2, "vp1"),
334         ACT88xx_REG("DCDC_REG2", ACT8865, DCDC2, VSET2, "vp2"),
335         ACT88xx_REG("DCDC_REG3", ACT8865, DCDC3, VSET2, "vp3"),
336         ACT88xx_REG("LDO_REG1", ACT8865, LDO1, VSET, "inl45"),
337         ACT88xx_REG("LDO_REG2", ACT8865, LDO2, VSET, "inl45"),
338         ACT88xx_REG("LDO_REG3", ACT8865, LDO3, VSET, "inl67"),
339         ACT88xx_REG("LDO_REG4", ACT8865, LDO4, VSET, "inl67"),
340 };
341
342 #ifdef CONFIG_OF
343 static const struct of_device_id act8865_dt_ids[] = {
344         { .compatible = "active-semi,act8600", .data = (void *)ACT8600 },
345         { .compatible = "active-semi,act8846", .data = (void *)ACT8846 },
346         { .compatible = "active-semi,act8865", .data = (void *)ACT8865 },
347         { }
348 };
349 MODULE_DEVICE_TABLE(of, act8865_dt_ids);
350 #endif
351
352 static struct act8865_regulator_data *act8865_get_regulator_data(
353                 int id, struct act8865_platform_data *pdata)
354 {
355         int i;
356
357         for (i = 0; i < pdata->num_regulators; i++) {
358                 if (pdata->regulators[i].id == id)
359                         return &pdata->regulators[i];
360         }
361
362         return NULL;
363 }
364
365 static struct i2c_client *act8865_i2c_client;
366 static void act8865_power_off(void)
367 {
368         struct act8865 *act8865;
369
370         act8865 = i2c_get_clientdata(act8865_i2c_client);
371         regmap_write(act8865->regmap, act8865->off_reg, act8865->off_mask);
372         while (1);
373 }
374
375 static int act8865_pmic_probe(struct i2c_client *client,
376                               const struct i2c_device_id *i2c_id)
377 {
378         const struct regulator_desc *regulators;
379         struct act8865_platform_data *pdata = NULL;
380         struct device *dev = &client->dev;
381         int i, ret, num_regulators;
382         struct act8865 *act8865;
383         const struct regmap_config *regmap_config;
384         unsigned long type;
385         int off_reg, off_mask;
386         int voltage_select = 0;
387
388         if (dev->of_node) {
389                 const struct of_device_id *id;
390
391                 id = of_match_device(of_match_ptr(act8865_dt_ids), dev);
392                 if (!id)
393                         return -ENODEV;
394
395                 type = (unsigned long) id->data;
396
397                 voltage_select = !!of_get_property(dev->of_node,
398                                                    "active-semi,vsel-high",
399                                                    NULL);
400         } else {
401                 type = i2c_id->driver_data;
402                 pdata = dev_get_platdata(dev);
403         }
404
405         switch (type) {
406         case ACT8600:
407                 regulators = act8600_regulators;
408                 num_regulators = ARRAY_SIZE(act8600_regulators);
409                 regmap_config = &act8600_regmap_config;
410                 off_reg = -1;
411                 off_mask = -1;
412                 break;
413         case ACT8846:
414                 regulators = act8846_regulators;
415                 num_regulators = ARRAY_SIZE(act8846_regulators);
416                 regmap_config = &act8865_regmap_config;
417                 off_reg = ACT8846_GLB_OFF_CTRL;
418                 off_mask = ACT8846_OFF_SYSMASK;
419                 break;
420         case ACT8865:
421                 if (voltage_select) {
422                         regulators = act8865_alt_regulators;
423                         num_regulators = ARRAY_SIZE(act8865_alt_regulators);
424                 } else {
425                         regulators = act8865_regulators;
426                         num_regulators = ARRAY_SIZE(act8865_regulators);
427                 }
428                 regmap_config = &act8865_regmap_config;
429                 off_reg = ACT8865_SYS_CTRL;
430                 off_mask = ACT8865_MSTROFF;
431                 break;
432         default:
433                 dev_err(dev, "invalid device id %lu\n", type);
434                 return -EINVAL;
435         }
436
437         act8865 = devm_kzalloc(dev, sizeof(struct act8865), GFP_KERNEL);
438         if (!act8865)
439                 return -ENOMEM;
440
441         act8865->regmap = devm_regmap_init_i2c(client, regmap_config);
442         if (IS_ERR(act8865->regmap)) {
443                 ret = PTR_ERR(act8865->regmap);
444                 dev_err(dev, "Failed to allocate register map: %d\n", ret);
445                 return ret;
446         }
447
448         if (of_device_is_system_power_controller(dev->of_node)) {
449                 if (!pm_power_off && (off_reg > 0)) {
450                         act8865_i2c_client = client;
451                         act8865->off_reg = off_reg;
452                         act8865->off_mask = off_mask;
453                         pm_power_off = act8865_power_off;
454                 } else {
455                         dev_err(dev, "Failed to set poweroff capability, already defined\n");
456                 }
457         }
458
459         /* Finally register devices */
460         for (i = 0; i < num_regulators; i++) {
461                 const struct regulator_desc *desc = &regulators[i];
462                 struct regulator_config config = { };
463                 struct regulator_dev *rdev;
464
465                 config.dev = dev;
466                 config.driver_data = act8865;
467                 config.regmap = act8865->regmap;
468
469                 if (pdata) {
470                         struct act8865_regulator_data *rdata;
471
472                         rdata = act8865_get_regulator_data(desc->id, pdata);
473                         if (rdata) {
474                                 config.init_data = rdata->init_data;
475                                 config.of_node = rdata->of_node;
476                         }
477                 }
478
479                 rdev = devm_regulator_register(dev, desc, &config);
480                 if (IS_ERR(rdev)) {
481                         dev_err(dev, "failed to register %s\n", desc->name);
482                         return PTR_ERR(rdev);
483                 }
484         }
485
486         i2c_set_clientdata(client, act8865);
487
488         return 0;
489 }
490
491 static const struct i2c_device_id act8865_ids[] = {
492         { .name = "act8600", .driver_data = ACT8600 },
493         { .name = "act8846", .driver_data = ACT8846 },
494         { .name = "act8865", .driver_data = ACT8865 },
495         { },
496 };
497 MODULE_DEVICE_TABLE(i2c, act8865_ids);
498
499 static struct i2c_driver act8865_pmic_driver = {
500         .driver = {
501                 .name   = "act8865",
502         },
503         .probe          = act8865_pmic_probe,
504         .id_table       = act8865_ids,
505 };
506
507 module_i2c_driver(act8865_pmic_driver);
508
509 MODULE_DESCRIPTION("active-semi act88xx voltage regulator driver");
510 MODULE_AUTHOR("Wenyou Yang <wenyou.yang@atmel.com>");
511 MODULE_LICENSE("GPL v2");