Version:  2.0.40 2.2.26 2.4.37 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5

Linux/drivers/regulator/lp872x.c

  1 /*
  2  * Copyright 2012 Texas Instruments
  3  *
  4  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  5  *
  6  * This program is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License version 2 as
  8  * published by the Free Software Foundation.
  9  *
 10  */
 11 
 12 #include <linux/module.h>
 13 #include <linux/slab.h>
 14 #include <linux/i2c.h>
 15 #include <linux/regmap.h>
 16 #include <linux/err.h>
 17 #include <linux/gpio.h>
 18 #include <linux/regulator/lp872x.h>
 19 #include <linux/regulator/driver.h>
 20 #include <linux/platform_device.h>
 21 #include <linux/of.h>
 22 #include <linux/of_gpio.h>
 23 #include <linux/regulator/of_regulator.h>
 24 
 25 /* Registers : LP8720/8725 shared */
 26 #define LP872X_GENERAL_CFG              0x00
 27 #define LP872X_LDO1_VOUT                0x01
 28 #define LP872X_LDO2_VOUT                0x02
 29 #define LP872X_LDO3_VOUT                0x03
 30 #define LP872X_LDO4_VOUT                0x04
 31 #define LP872X_LDO5_VOUT                0x05
 32 
 33 /* Registers : LP8720 */
 34 #define LP8720_BUCK_VOUT1               0x06
 35 #define LP8720_BUCK_VOUT2               0x07
 36 #define LP8720_ENABLE                   0x08
 37 
 38 /* Registers : LP8725 */
 39 #define LP8725_LILO1_VOUT               0x06
 40 #define LP8725_LILO2_VOUT               0x07
 41 #define LP8725_BUCK1_VOUT1              0x08
 42 #define LP8725_BUCK1_VOUT2              0x09
 43 #define LP8725_BUCK2_VOUT1              0x0A
 44 #define LP8725_BUCK2_VOUT2              0x0B
 45 #define LP8725_BUCK_CTRL                0x0C
 46 #define LP8725_LDO_CTRL                 0x0D
 47 
 48 /* Mask/shift : LP8720/LP8725 shared */
 49 #define LP872X_VOUT_M                   0x1F
 50 #define LP872X_START_DELAY_M            0xE0
 51 #define LP872X_START_DELAY_S            5
 52 #define LP872X_EN_LDO1_M                BIT(0)
 53 #define LP872X_EN_LDO2_M                BIT(1)
 54 #define LP872X_EN_LDO3_M                BIT(2)
 55 #define LP872X_EN_LDO4_M                BIT(3)
 56 #define LP872X_EN_LDO5_M                BIT(4)
 57 
 58 /* Mask/shift : LP8720 */
 59 #define LP8720_TIMESTEP_S               0               /* Addr 00h */
 60 #define LP8720_TIMESTEP_M               BIT(0)
 61 #define LP8720_EXT_DVS_M                BIT(2)
 62 #define LP8720_BUCK_FPWM_S              5               /* Addr 07h */
 63 #define LP8720_BUCK_FPWM_M              BIT(5)
 64 #define LP8720_EN_BUCK_M                BIT(5)          /* Addr 08h */
 65 #define LP8720_DVS_SEL_M                BIT(7)
 66 
 67 /* Mask/shift : LP8725 */
 68 #define LP8725_TIMESTEP_M               0xC0            /* Addr 00h */
 69 #define LP8725_TIMESTEP_S               6
 70 #define LP8725_BUCK1_EN_M               BIT(0)
 71 #define LP8725_DVS1_M                   BIT(2)
 72 #define LP8725_DVS2_M                   BIT(3)
 73 #define LP8725_BUCK2_EN_M               BIT(4)
 74 #define LP8725_BUCK_CL_M                0xC0            /* Addr 09h, 0Bh */
 75 #define LP8725_BUCK_CL_S                6
 76 #define LP8725_BUCK1_FPWM_S             1               /* Addr 0Ch */
 77 #define LP8725_BUCK1_FPWM_M             BIT(1)
 78 #define LP8725_BUCK2_FPWM_S             5
 79 #define LP8725_BUCK2_FPWM_M             BIT(5)
 80 #define LP8725_EN_LILO1_M               BIT(5)          /* Addr 0Dh */
 81 #define LP8725_EN_LILO2_M               BIT(6)
 82 
 83 /* PWM mode */
 84 #define LP872X_FORCE_PWM                1
 85 #define LP872X_AUTO_PWM                 0
 86 
 87 #define LP8720_NUM_REGULATORS           6
 88 #define LP8725_NUM_REGULATORS           9
 89 #define EXTERN_DVS_USED                 0
 90 #define MAX_DELAY                       6
 91 
 92 /* Default DVS Mode */
 93 #define LP8720_DEFAULT_DVS              0
 94 #define LP8725_DEFAULT_DVS              BIT(2)
 95 
 96 /* dump registers in regmap-debugfs */
 97 #define MAX_REGISTERS                   0x0F
 98 
 99 enum lp872x_id {
100         LP8720,
101         LP8725,
102 };
103 
104 struct lp872x {
105         struct regmap *regmap;
106         struct device *dev;
107         enum lp872x_id chipid;
108         struct lp872x_platform_data *pdata;
109         int num_regulators;
110         enum lp872x_dvs_state dvs_pin;
111 };
112 
113 /* LP8720/LP8725 shared voltage table for LDOs */
114 static const unsigned int lp872x_ldo_vtbl[] = {
115         1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, 1550000,
116         1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000, 2000000,
117         2100000, 2200000, 2300000, 2400000, 2500000, 2600000, 2650000, 2700000,
118         2750000, 2800000, 2850000, 2900000, 2950000, 3000000, 3100000, 3300000,
119 };
120 
121 /* LP8720 LDO4 voltage table */
122 static const unsigned int lp8720_ldo4_vtbl[] = {
123          800000,  850000,  900000, 1000000, 1100000, 1200000, 1250000, 1300000,
124         1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
125         1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
126         2400000, 2500000, 2600000, 2650000, 2700000, 2750000, 2800000, 2850000,
127 };
128 
129 /* LP8725 LILO(Low Input Low Output) voltage table */
130 static const unsigned int lp8725_lilo_vtbl[] = {
131          800000,  850000,  900000,  950000, 1000000, 1050000, 1100000, 1150000,
132         1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
133         1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
134         2600000, 2700000, 2800000, 2850000, 2900000, 3000000, 3100000, 3300000,
135 };
136 
137 /* LP8720 BUCK voltage table */
138 #define EXT_R           0       /* external resistor divider */
139 static const unsigned int lp8720_buck_vtbl[] = {
140           EXT_R,  800000,  850000,  900000,  950000, 1000000, 1050000, 1100000,
141         1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
142         1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000, 1900000,
143         1950000, 2000000, 2050000, 2100000, 2150000, 2200000, 2250000, 2300000,
144 };
145 
146 /* LP8725 BUCK voltage table */
147 static const unsigned int lp8725_buck_vtbl[] = {
148          800000,  850000,  900000,  950000, 1000000, 1050000, 1100000, 1150000,
149         1200000, 1250000, 1300000, 1350000, 1400000, 1500000, 1600000, 1700000,
150         1750000, 1800000, 1850000, 1900000, 2000000, 2100000, 2200000, 2300000,
151         2400000, 2500000, 2600000, 2700000, 2800000, 2850000, 2900000, 3000000,
152 };
153 
154 /* LP8725 BUCK current limit */
155 static const unsigned int lp8725_buck_uA[] = {
156         460000, 780000, 1050000, 1370000,
157 };
158 
159 static int lp872x_read_byte(struct lp872x *lp, u8 addr, u8 *data)
160 {
161         int ret;
162         unsigned int val;
163 
164         ret = regmap_read(lp->regmap, addr, &val);
165         if (ret < 0) {
166                 dev_err(lp->dev, "failed to read 0x%.2x\n", addr);
167                 return ret;
168         }
169 
170         *data = (u8)val;
171         return 0;
172 }
173 
174 static inline int lp872x_write_byte(struct lp872x *lp, u8 addr, u8 data)
175 {
176         return regmap_write(lp->regmap, addr, data);
177 }
178 
179 static inline int lp872x_update_bits(struct lp872x *lp, u8 addr,
180                                 unsigned int mask, u8 data)
181 {
182         return regmap_update_bits(lp->regmap, addr, mask, data);
183 }
184 
185 static int lp872x_get_timestep_usec(struct lp872x *lp)
186 {
187         enum lp872x_id chip = lp->chipid;
188         u8 val, mask, shift;
189         int *time_usec, size, ret;
190         int lp8720_time_usec[] = { 25, 50 };
191         int lp8725_time_usec[] = { 32, 64, 128, 256 };
192 
193         switch (chip) {
194         case LP8720:
195                 mask = LP8720_TIMESTEP_M;
196                 shift = LP8720_TIMESTEP_S;
197                 time_usec = &lp8720_time_usec[0];
198                 size = ARRAY_SIZE(lp8720_time_usec);
199                 break;
200         case LP8725:
201                 mask = LP8725_TIMESTEP_M;
202                 shift = LP8725_TIMESTEP_S;
203                 time_usec = &lp8725_time_usec[0];
204                 size = ARRAY_SIZE(lp8725_time_usec);
205                 break;
206         default:
207                 return -EINVAL;
208         }
209 
210         ret = lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val);
211         if (ret)
212                 return ret;
213 
214         val = (val & mask) >> shift;
215         if (val >= size)
216                 return -EINVAL;
217 
218         return *(time_usec + val);
219 }
220 
221 static int lp872x_regulator_enable_time(struct regulator_dev *rdev)
222 {
223         struct lp872x *lp = rdev_get_drvdata(rdev);
224         enum lp872x_regulator_id rid = rdev_get_id(rdev);
225         int time_step_us = lp872x_get_timestep_usec(lp);
226         int ret;
227         u8 addr, val;
228 
229         if (time_step_us < 0)
230                 return time_step_us;
231 
232         switch (rid) {
233         case LP8720_ID_LDO1 ... LP8720_ID_BUCK:
234                 addr = LP872X_LDO1_VOUT + rid;
235                 break;
236         case LP8725_ID_LDO1 ... LP8725_ID_BUCK1:
237                 addr = LP872X_LDO1_VOUT + rid - LP8725_ID_BASE;
238                 break;
239         case LP8725_ID_BUCK2:
240                 addr = LP8725_BUCK2_VOUT1;
241                 break;
242         default:
243                 return -EINVAL;
244         }
245 
246         ret = lp872x_read_byte(lp, addr, &val);
247         if (ret)
248                 return ret;
249 
250         val = (val & LP872X_START_DELAY_M) >> LP872X_START_DELAY_S;
251 
252         return val > MAX_DELAY ? 0 : val * time_step_us;
253 }
254 
255 static void lp872x_set_dvs(struct lp872x *lp, enum lp872x_dvs_sel dvs_sel,
256                         int gpio)
257 {
258         enum lp872x_dvs_state state;
259 
260         state = dvs_sel == SEL_V1 ? DVS_HIGH : DVS_LOW;
261         gpio_set_value(gpio, state);
262         lp->dvs_pin = state;
263 }
264 
265 static u8 lp872x_select_buck_vout_addr(struct lp872x *lp,
266                                 enum lp872x_regulator_id buck)
267 {
268         u8 val, addr;
269 
270         if (lp872x_read_byte(lp, LP872X_GENERAL_CFG, &val))
271                 return 0;
272 
273         switch (buck) {
274         case LP8720_ID_BUCK:
275                 if (val & LP8720_EXT_DVS_M) {
276                         addr = (lp->dvs_pin == DVS_HIGH) ?
277                                 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
278                 } else {
279                         if (lp872x_read_byte(lp, LP8720_ENABLE, &val))
280                                 return 0;
281 
282                         addr = val & LP8720_DVS_SEL_M ?
283                                 LP8720_BUCK_VOUT1 : LP8720_BUCK_VOUT2;
284                 }
285                 break;
286         case LP8725_ID_BUCK1:
287                 if (val & LP8725_DVS1_M)
288                         addr = LP8725_BUCK1_VOUT1;
289                 else
290                         addr = (lp->dvs_pin == DVS_HIGH) ?
291                                 LP8725_BUCK1_VOUT1 : LP8725_BUCK1_VOUT2;
292                 break;
293         case LP8725_ID_BUCK2:
294                 addr =  val & LP8725_DVS2_M ?
295                         LP8725_BUCK2_VOUT1 : LP8725_BUCK2_VOUT2;
296                 break;
297         default:
298                 return 0;
299         }
300 
301         return addr;
302 }
303 
304 static bool lp872x_is_valid_buck_addr(u8 addr)
305 {
306         switch (addr) {
307         case LP8720_BUCK_VOUT1:
308         case LP8720_BUCK_VOUT2:
309         case LP8725_BUCK1_VOUT1:
310         case LP8725_BUCK1_VOUT2:
311         case LP8725_BUCK2_VOUT1:
312         case LP8725_BUCK2_VOUT2:
313                 return true;
314         default:
315                 return false;
316         }
317 }
318 
319 static int lp872x_buck_set_voltage_sel(struct regulator_dev *rdev,
320                                         unsigned selector)
321 {
322         struct lp872x *lp = rdev_get_drvdata(rdev);
323         enum lp872x_regulator_id buck = rdev_get_id(rdev);
324         u8 addr, mask = LP872X_VOUT_M;
325         struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
326 
327         if (dvs && gpio_is_valid(dvs->gpio))
328                 lp872x_set_dvs(lp, dvs->vsel, dvs->gpio);
329 
330         addr = lp872x_select_buck_vout_addr(lp, buck);
331         if (!lp872x_is_valid_buck_addr(addr))
332                 return -EINVAL;
333 
334         return lp872x_update_bits(lp, addr, mask, selector);
335 }
336 
337 static int lp872x_buck_get_voltage_sel(struct regulator_dev *rdev)
338 {
339         struct lp872x *lp = rdev_get_drvdata(rdev);
340         enum lp872x_regulator_id buck = rdev_get_id(rdev);
341         u8 addr, val;
342         int ret;
343 
344         addr = lp872x_select_buck_vout_addr(lp, buck);
345         if (!lp872x_is_valid_buck_addr(addr))
346                 return -EINVAL;
347 
348         ret = lp872x_read_byte(lp, addr, &val);
349         if (ret)
350                 return ret;
351 
352         return val & LP872X_VOUT_M;
353 }
354 
355 static int lp8725_buck_set_current_limit(struct regulator_dev *rdev,
356                                         int min_uA, int max_uA)
357 {
358         struct lp872x *lp = rdev_get_drvdata(rdev);
359         enum lp872x_regulator_id buck = rdev_get_id(rdev);
360         int i;
361         u8 addr;
362 
363         switch (buck) {
364         case LP8725_ID_BUCK1:
365                 addr = LP8725_BUCK1_VOUT2;
366                 break;
367         case LP8725_ID_BUCK2:
368                 addr = LP8725_BUCK2_VOUT2;
369                 break;
370         default:
371                 return -EINVAL;
372         }
373 
374         for (i = ARRAY_SIZE(lp8725_buck_uA) - 1; i >= 0; i--) {
375                 if (lp8725_buck_uA[i] >= min_uA &&
376                         lp8725_buck_uA[i] <= max_uA)
377                         return lp872x_update_bits(lp, addr,
378                                                   LP8725_BUCK_CL_M,
379                                                   i << LP8725_BUCK_CL_S);
380         }
381 
382         return -EINVAL;
383 }
384 
385 static int lp8725_buck_get_current_limit(struct regulator_dev *rdev)
386 {
387         struct lp872x *lp = rdev_get_drvdata(rdev);
388         enum lp872x_regulator_id buck = rdev_get_id(rdev);
389         u8 addr, val;
390         int ret;
391 
392         switch (buck) {
393         case LP8725_ID_BUCK1:
394                 addr = LP8725_BUCK1_VOUT2;
395                 break;
396         case LP8725_ID_BUCK2:
397                 addr = LP8725_BUCK2_VOUT2;
398                 break;
399         default:
400                 return -EINVAL;
401         }
402 
403         ret = lp872x_read_byte(lp, addr, &val);
404         if (ret)
405                 return ret;
406 
407         val = (val & LP8725_BUCK_CL_M) >> LP8725_BUCK_CL_S;
408 
409         return (val < ARRAY_SIZE(lp8725_buck_uA)) ?
410                         lp8725_buck_uA[val] : -EINVAL;
411 }
412 
413 static int lp872x_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
414 {
415         struct lp872x *lp = rdev_get_drvdata(rdev);
416         enum lp872x_regulator_id buck = rdev_get_id(rdev);
417         u8 addr, mask, shift, val;
418 
419         switch (buck) {
420         case LP8720_ID_BUCK:
421                 addr = LP8720_BUCK_VOUT2;
422                 mask = LP8720_BUCK_FPWM_M;
423                 shift = LP8720_BUCK_FPWM_S;
424                 break;
425         case LP8725_ID_BUCK1:
426                 addr = LP8725_BUCK_CTRL;
427                 mask = LP8725_BUCK1_FPWM_M;
428                 shift = LP8725_BUCK1_FPWM_S;
429                 break;
430         case LP8725_ID_BUCK2:
431                 addr = LP8725_BUCK_CTRL;
432                 mask = LP8725_BUCK2_FPWM_M;
433                 shift = LP8725_BUCK2_FPWM_S;
434                 break;
435         default:
436                 return -EINVAL;
437         }
438 
439         if (mode == REGULATOR_MODE_FAST)
440                 val = LP872X_FORCE_PWM << shift;
441         else if (mode == REGULATOR_MODE_NORMAL)
442                 val = LP872X_AUTO_PWM << shift;
443         else
444                 return -EINVAL;
445 
446         return lp872x_update_bits(lp, addr, mask, val);
447 }
448 
449 static unsigned int lp872x_buck_get_mode(struct regulator_dev *rdev)
450 {
451         struct lp872x *lp = rdev_get_drvdata(rdev);
452         enum lp872x_regulator_id buck = rdev_get_id(rdev);
453         u8 addr, mask, val;
454         int ret;
455 
456         switch (buck) {
457         case LP8720_ID_BUCK:
458                 addr = LP8720_BUCK_VOUT2;
459                 mask = LP8720_BUCK_FPWM_M;
460                 break;
461         case LP8725_ID_BUCK1:
462                 addr = LP8725_BUCK_CTRL;
463                 mask = LP8725_BUCK1_FPWM_M;
464                 break;
465         case LP8725_ID_BUCK2:
466                 addr = LP8725_BUCK_CTRL;
467                 mask = LP8725_BUCK2_FPWM_M;
468                 break;
469         default:
470                 return -EINVAL;
471         }
472 
473         ret = lp872x_read_byte(lp, addr, &val);
474         if (ret)
475                 return ret;
476 
477         return val & mask ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
478 }
479 
480 static struct regulator_ops lp872x_ldo_ops = {
481         .list_voltage = regulator_list_voltage_table,
482         .map_voltage = regulator_map_voltage_ascend,
483         .set_voltage_sel = regulator_set_voltage_sel_regmap,
484         .get_voltage_sel = regulator_get_voltage_sel_regmap,
485         .enable = regulator_enable_regmap,
486         .disable = regulator_disable_regmap,
487         .is_enabled = regulator_is_enabled_regmap,
488         .enable_time = lp872x_regulator_enable_time,
489 };
490 
491 static struct regulator_ops lp8720_buck_ops = {
492         .list_voltage = regulator_list_voltage_table,
493         .map_voltage = regulator_map_voltage_ascend,
494         .set_voltage_sel = lp872x_buck_set_voltage_sel,
495         .get_voltage_sel = lp872x_buck_get_voltage_sel,
496         .enable = regulator_enable_regmap,
497         .disable = regulator_disable_regmap,
498         .is_enabled = regulator_is_enabled_regmap,
499         .enable_time = lp872x_regulator_enable_time,
500         .set_mode = lp872x_buck_set_mode,
501         .get_mode = lp872x_buck_get_mode,
502 };
503 
504 static struct regulator_ops lp8725_buck_ops = {
505         .list_voltage = regulator_list_voltage_table,
506         .map_voltage = regulator_map_voltage_ascend,
507         .set_voltage_sel = lp872x_buck_set_voltage_sel,
508         .get_voltage_sel = lp872x_buck_get_voltage_sel,
509         .enable = regulator_enable_regmap,
510         .disable = regulator_disable_regmap,
511         .is_enabled = regulator_is_enabled_regmap,
512         .enable_time = lp872x_regulator_enable_time,
513         .set_mode = lp872x_buck_set_mode,
514         .get_mode = lp872x_buck_get_mode,
515         .set_current_limit = lp8725_buck_set_current_limit,
516         .get_current_limit = lp8725_buck_get_current_limit,
517 };
518 
519 static struct regulator_desc lp8720_regulator_desc[] = {
520         {
521                 .name = "ldo1",
522                 .of_match = of_match_ptr("ldo1"),
523                 .id = LP8720_ID_LDO1,
524                 .ops = &lp872x_ldo_ops,
525                 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
526                 .volt_table = lp872x_ldo_vtbl,
527                 .type = REGULATOR_VOLTAGE,
528                 .owner = THIS_MODULE,
529                 .vsel_reg = LP872X_LDO1_VOUT,
530                 .vsel_mask = LP872X_VOUT_M,
531                 .enable_reg = LP8720_ENABLE,
532                 .enable_mask = LP872X_EN_LDO1_M,
533         },
534         {
535                 .name = "ldo2",
536                 .of_match = of_match_ptr("ldo2"),
537                 .id = LP8720_ID_LDO2,
538                 .ops = &lp872x_ldo_ops,
539                 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
540                 .volt_table = lp872x_ldo_vtbl,
541                 .type = REGULATOR_VOLTAGE,
542                 .owner = THIS_MODULE,
543                 .vsel_reg = LP872X_LDO2_VOUT,
544                 .vsel_mask = LP872X_VOUT_M,
545                 .enable_reg = LP8720_ENABLE,
546                 .enable_mask = LP872X_EN_LDO2_M,
547         },
548         {
549                 .name = "ldo3",
550                 .of_match = of_match_ptr("ldo3"),
551                 .id = LP8720_ID_LDO3,
552                 .ops = &lp872x_ldo_ops,
553                 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
554                 .volt_table = lp872x_ldo_vtbl,
555                 .type = REGULATOR_VOLTAGE,
556                 .owner = THIS_MODULE,
557                 .vsel_reg = LP872X_LDO3_VOUT,
558                 .vsel_mask = LP872X_VOUT_M,
559                 .enable_reg = LP8720_ENABLE,
560                 .enable_mask = LP872X_EN_LDO3_M,
561         },
562         {
563                 .name = "ldo4",
564                 .of_match = of_match_ptr("ldo4"),
565                 .id = LP8720_ID_LDO4,
566                 .ops = &lp872x_ldo_ops,
567                 .n_voltages = ARRAY_SIZE(lp8720_ldo4_vtbl),
568                 .volt_table = lp8720_ldo4_vtbl,
569                 .type = REGULATOR_VOLTAGE,
570                 .owner = THIS_MODULE,
571                 .vsel_reg = LP872X_LDO4_VOUT,
572                 .vsel_mask = LP872X_VOUT_M,
573                 .enable_reg = LP8720_ENABLE,
574                 .enable_mask = LP872X_EN_LDO4_M,
575         },
576         {
577                 .name = "ldo5",
578                 .of_match = of_match_ptr("ldo5"),
579                 .id = LP8720_ID_LDO5,
580                 .ops = &lp872x_ldo_ops,
581                 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
582                 .volt_table = lp872x_ldo_vtbl,
583                 .type = REGULATOR_VOLTAGE,
584                 .owner = THIS_MODULE,
585                 .vsel_reg = LP872X_LDO5_VOUT,
586                 .vsel_mask = LP872X_VOUT_M,
587                 .enable_reg = LP8720_ENABLE,
588                 .enable_mask = LP872X_EN_LDO5_M,
589         },
590         {
591                 .name = "buck",
592                 .of_match = of_match_ptr("buck"),
593                 .id = LP8720_ID_BUCK,
594                 .ops = &lp8720_buck_ops,
595                 .n_voltages = ARRAY_SIZE(lp8720_buck_vtbl),
596                 .volt_table = lp8720_buck_vtbl,
597                 .type = REGULATOR_VOLTAGE,
598                 .owner = THIS_MODULE,
599                 .enable_reg = LP8720_ENABLE,
600                 .enable_mask = LP8720_EN_BUCK_M,
601         },
602 };
603 
604 static struct regulator_desc lp8725_regulator_desc[] = {
605         {
606                 .name = "ldo1",
607                 .of_match = of_match_ptr("ldo1"),
608                 .id = LP8725_ID_LDO1,
609                 .ops = &lp872x_ldo_ops,
610                 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
611                 .volt_table = lp872x_ldo_vtbl,
612                 .type = REGULATOR_VOLTAGE,
613                 .owner = THIS_MODULE,
614                 .vsel_reg = LP872X_LDO1_VOUT,
615                 .vsel_mask = LP872X_VOUT_M,
616                 .enable_reg = LP8725_LDO_CTRL,
617                 .enable_mask = LP872X_EN_LDO1_M,
618         },
619         {
620                 .name = "ldo2",
621                 .of_match = of_match_ptr("ldo2"),
622                 .id = LP8725_ID_LDO2,
623                 .ops = &lp872x_ldo_ops,
624                 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
625                 .volt_table = lp872x_ldo_vtbl,
626                 .type = REGULATOR_VOLTAGE,
627                 .owner = THIS_MODULE,
628                 .vsel_reg = LP872X_LDO2_VOUT,
629                 .vsel_mask = LP872X_VOUT_M,
630                 .enable_reg = LP8725_LDO_CTRL,
631                 .enable_mask = LP872X_EN_LDO2_M,
632         },
633         {
634                 .name = "ldo3",
635                 .of_match = of_match_ptr("ldo3"),
636                 .id = LP8725_ID_LDO3,
637                 .ops = &lp872x_ldo_ops,
638                 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
639                 .volt_table = lp872x_ldo_vtbl,
640                 .type = REGULATOR_VOLTAGE,
641                 .owner = THIS_MODULE,
642                 .vsel_reg = LP872X_LDO3_VOUT,
643                 .vsel_mask = LP872X_VOUT_M,
644                 .enable_reg = LP8725_LDO_CTRL,
645                 .enable_mask = LP872X_EN_LDO3_M,
646         },
647         {
648                 .name = "ldo4",
649                 .of_match = of_match_ptr("ldo4"),
650                 .id = LP8725_ID_LDO4,
651                 .ops = &lp872x_ldo_ops,
652                 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
653                 .volt_table = lp872x_ldo_vtbl,
654                 .type = REGULATOR_VOLTAGE,
655                 .owner = THIS_MODULE,
656                 .vsel_reg = LP872X_LDO4_VOUT,
657                 .vsel_mask = LP872X_VOUT_M,
658                 .enable_reg = LP8725_LDO_CTRL,
659                 .enable_mask = LP872X_EN_LDO4_M,
660         },
661         {
662                 .name = "ldo5",
663                 .of_match = of_match_ptr("ldo5"),
664                 .id = LP8725_ID_LDO5,
665                 .ops = &lp872x_ldo_ops,
666                 .n_voltages = ARRAY_SIZE(lp872x_ldo_vtbl),
667                 .volt_table = lp872x_ldo_vtbl,
668                 .type = REGULATOR_VOLTAGE,
669                 .owner = THIS_MODULE,
670                 .vsel_reg = LP872X_LDO5_VOUT,
671                 .vsel_mask = LP872X_VOUT_M,
672                 .enable_reg = LP8725_LDO_CTRL,
673                 .enable_mask = LP872X_EN_LDO5_M,
674         },
675         {
676                 .name = "lilo1",
677                 .of_match = of_match_ptr("lilo1"),
678                 .id = LP8725_ID_LILO1,
679                 .ops = &lp872x_ldo_ops,
680                 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
681                 .volt_table = lp8725_lilo_vtbl,
682                 .type = REGULATOR_VOLTAGE,
683                 .owner = THIS_MODULE,
684                 .vsel_reg = LP8725_LILO1_VOUT,
685                 .vsel_mask = LP872X_VOUT_M,
686                 .enable_reg = LP8725_LDO_CTRL,
687                 .enable_mask = LP8725_EN_LILO1_M,
688         },
689         {
690                 .name = "lilo2",
691                 .of_match = of_match_ptr("lilo2"),
692                 .id = LP8725_ID_LILO2,
693                 .ops = &lp872x_ldo_ops,
694                 .n_voltages = ARRAY_SIZE(lp8725_lilo_vtbl),
695                 .volt_table = lp8725_lilo_vtbl,
696                 .type = REGULATOR_VOLTAGE,
697                 .owner = THIS_MODULE,
698                 .vsel_reg = LP8725_LILO2_VOUT,
699                 .vsel_mask = LP872X_VOUT_M,
700                 .enable_reg = LP8725_LDO_CTRL,
701                 .enable_mask = LP8725_EN_LILO2_M,
702         },
703         {
704                 .name = "buck1",
705                 .of_match = of_match_ptr("buck1"),
706                 .id = LP8725_ID_BUCK1,
707                 .ops = &lp8725_buck_ops,
708                 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
709                 .volt_table = lp8725_buck_vtbl,
710                 .type = REGULATOR_VOLTAGE,
711                 .owner = THIS_MODULE,
712                 .enable_reg = LP872X_GENERAL_CFG,
713                 .enable_mask = LP8725_BUCK1_EN_M,
714         },
715         {
716                 .name = "buck2",
717                 .of_match = of_match_ptr("buck2"),
718                 .id = LP8725_ID_BUCK2,
719                 .ops = &lp8725_buck_ops,
720                 .n_voltages = ARRAY_SIZE(lp8725_buck_vtbl),
721                 .volt_table = lp8725_buck_vtbl,
722                 .type = REGULATOR_VOLTAGE,
723                 .owner = THIS_MODULE,
724                 .enable_reg = LP872X_GENERAL_CFG,
725                 .enable_mask = LP8725_BUCK2_EN_M,
726         },
727 };
728 
729 static int lp872x_init_dvs(struct lp872x *lp)
730 {
731         int ret, gpio;
732         struct lp872x_dvs *dvs = lp->pdata ? lp->pdata->dvs : NULL;
733         enum lp872x_dvs_state pinstate;
734         u8 mask[] = { LP8720_EXT_DVS_M, LP8725_DVS1_M | LP8725_DVS2_M };
735         u8 default_dvs_mode[] = { LP8720_DEFAULT_DVS, LP8725_DEFAULT_DVS };
736 
737         if (!dvs)
738                 goto set_default_dvs_mode;
739 
740         gpio = dvs->gpio;
741         if (!gpio_is_valid(gpio)) {
742                 dev_warn(lp->dev, "invalid gpio: %d\n", gpio);
743                 goto set_default_dvs_mode;
744         }
745 
746         pinstate = dvs->init_state;
747         ret = devm_gpio_request_one(lp->dev, gpio, pinstate, "LP872X DVS");
748         if (ret) {
749                 dev_err(lp->dev, "gpio request err: %d\n", ret);
750                 return ret;
751         }
752 
753         lp->dvs_pin = pinstate;
754 
755         return 0;
756 
757 set_default_dvs_mode:
758         return lp872x_update_bits(lp, LP872X_GENERAL_CFG, mask[lp->chipid],
759                                 default_dvs_mode[lp->chipid]);
760 }
761 
762 static int lp872x_config(struct lp872x *lp)
763 {
764         struct lp872x_platform_data *pdata = lp->pdata;
765         int ret;
766 
767         if (!pdata || !pdata->update_config)
768                 goto init_dvs;
769 
770         ret = lp872x_write_byte(lp, LP872X_GENERAL_CFG, pdata->general_config);
771         if (ret)
772                 return ret;
773 
774 init_dvs:
775         return lp872x_init_dvs(lp);
776 }
777 
778 static struct regulator_init_data
779 *lp872x_find_regulator_init_data(int id, struct lp872x *lp)
780 {
781         struct lp872x_platform_data *pdata = lp->pdata;
782         int i;
783 
784         if (!pdata)
785                 return NULL;
786 
787         for (i = 0; i < lp->num_regulators; i++) {
788                 if (pdata->regulator_data[i].id == id)
789                         return pdata->regulator_data[i].init_data;
790         }
791 
792         return NULL;
793 }
794 
795 static int lp872x_regulator_register(struct lp872x *lp)
796 {
797         struct regulator_desc *desc;
798         struct regulator_config cfg = { };
799         struct regulator_dev *rdev;
800         int i;
801 
802         for (i = 0; i < lp->num_regulators; i++) {
803                 desc = (lp->chipid == LP8720) ? &lp8720_regulator_desc[i] :
804                                                 &lp8725_regulator_desc[i];
805 
806                 cfg.dev = lp->dev;
807                 cfg.init_data = lp872x_find_regulator_init_data(desc->id, lp);
808                 cfg.driver_data = lp;
809                 cfg.regmap = lp->regmap;
810 
811                 rdev = devm_regulator_register(lp->dev, desc, &cfg);
812                 if (IS_ERR(rdev)) {
813                         dev_err(lp->dev, "regulator register err");
814                         return PTR_ERR(rdev);
815                 }
816         }
817 
818         return 0;
819 }
820 
821 static const struct regmap_config lp872x_regmap_config = {
822         .reg_bits = 8,
823         .val_bits = 8,
824         .max_register = MAX_REGISTERS,
825 };
826 
827 #ifdef CONFIG_OF
828 
829 #define LP872X_VALID_OPMODE     (REGULATOR_MODE_FAST | REGULATOR_MODE_NORMAL)
830 
831 static struct of_regulator_match lp8720_matches[] = {
832         { .name = "ldo1", .driver_data = (void *)LP8720_ID_LDO1, },
833         { .name = "ldo2", .driver_data = (void *)LP8720_ID_LDO2, },
834         { .name = "ldo3", .driver_data = (void *)LP8720_ID_LDO3, },
835         { .name = "ldo4", .driver_data = (void *)LP8720_ID_LDO4, },
836         { .name = "ldo5", .driver_data = (void *)LP8720_ID_LDO5, },
837         { .name = "buck", .driver_data = (void *)LP8720_ID_BUCK, },
838 };
839 
840 static struct of_regulator_match lp8725_matches[] = {
841         { .name = "ldo1", .driver_data = (void *)LP8725_ID_LDO1, },
842         { .name = "ldo2", .driver_data = (void *)LP8725_ID_LDO2, },
843         { .name = "ldo3", .driver_data = (void *)LP8725_ID_LDO3, },
844         { .name = "ldo4", .driver_data = (void *)LP8725_ID_LDO4, },
845         { .name = "ldo5", .driver_data = (void *)LP8725_ID_LDO5, },
846         { .name = "lilo1", .driver_data = (void *)LP8725_ID_LILO1, },
847         { .name = "lilo2", .driver_data = (void *)LP8725_ID_LILO2, },
848         { .name = "buck1", .driver_data = (void *)LP8725_ID_BUCK1, },
849         { .name = "buck2", .driver_data = (void *)LP8725_ID_BUCK2, },
850 };
851 
852 static struct lp872x_platform_data
853 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
854 {
855         struct device_node *np = dev->of_node;
856         struct lp872x_platform_data *pdata;
857         struct of_regulator_match *match;
858         int num_matches;
859         int count;
860         int i;
861         u8 dvs_state;
862 
863         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
864         if (!pdata)
865                 return ERR_PTR(-ENOMEM);
866 
867         of_property_read_u8(np, "ti,general-config", &pdata->general_config);
868         if (of_find_property(np, "ti,update-config", NULL))
869                 pdata->update_config = true;
870 
871         pdata->dvs = devm_kzalloc(dev, sizeof(struct lp872x_dvs), GFP_KERNEL);
872         if (!pdata->dvs)
873                 return ERR_PTR(-ENOMEM);
874 
875         pdata->dvs->gpio = of_get_named_gpio(np, "ti,dvs-gpio", 0);
876         of_property_read_u8(np, "ti,dvs-vsel", (u8 *)&pdata->dvs->vsel);
877         of_property_read_u8(np, "ti,dvs-state", &dvs_state);
878         pdata->dvs->init_state = dvs_state ? DVS_HIGH : DVS_LOW;
879 
880         if (of_get_child_count(np) == 0)
881                 goto out;
882 
883         switch (which) {
884         case LP8720:
885                 match = lp8720_matches;
886                 num_matches = ARRAY_SIZE(lp8720_matches);
887                 break;
888         case LP8725:
889                 match = lp8725_matches;
890                 num_matches = ARRAY_SIZE(lp8725_matches);
891                 break;
892         default:
893                 goto out;
894         }
895 
896         count = of_regulator_match(dev, np, match, num_matches);
897         if (count <= 0)
898                 goto out;
899 
900         for (i = 0; i < num_matches; i++) {
901                 pdata->regulator_data[i].id =
902                                 (enum lp872x_regulator_id)match[i].driver_data;
903                 pdata->regulator_data[i].init_data = match[i].init_data;
904         }
905 out:
906         return pdata;
907 }
908 #else
909 static struct lp872x_platform_data
910 *lp872x_populate_pdata_from_dt(struct device *dev, enum lp872x_id which)
911 {
912         return NULL;
913 }
914 #endif
915 
916 static int lp872x_probe(struct i2c_client *cl, const struct i2c_device_id *id)
917 {
918         struct lp872x *lp;
919         struct lp872x_platform_data *pdata;
920         int ret;
921         const int lp872x_num_regulators[] = {
922                 [LP8720] = LP8720_NUM_REGULATORS,
923                 [LP8725] = LP8725_NUM_REGULATORS,
924         };
925 
926         if (cl->dev.of_node) {
927                 pdata = lp872x_populate_pdata_from_dt(&cl->dev,
928                                               (enum lp872x_id)id->driver_data);
929                 if (IS_ERR(pdata))
930                         return PTR_ERR(pdata);
931         } else {
932                 pdata = dev_get_platdata(&cl->dev);
933         }
934 
935         lp = devm_kzalloc(&cl->dev, sizeof(struct lp872x), GFP_KERNEL);
936         if (!lp)
937                 return -ENOMEM;
938 
939         lp->num_regulators = lp872x_num_regulators[id->driver_data];
940 
941         lp->regmap = devm_regmap_init_i2c(cl, &lp872x_regmap_config);
942         if (IS_ERR(lp->regmap)) {
943                 ret = PTR_ERR(lp->regmap);
944                 dev_err(&cl->dev, "regmap init i2c err: %d\n", ret);
945                 return ret;
946         }
947 
948         lp->dev = &cl->dev;
949         lp->pdata = pdata;
950         lp->chipid = id->driver_data;
951         i2c_set_clientdata(cl, lp);
952 
953         ret = lp872x_config(lp);
954         if (ret)
955                 return ret;
956 
957         return lp872x_regulator_register(lp);
958 }
959 
960 static const struct of_device_id lp872x_dt_ids[] = {
961         { .compatible = "ti,lp8720", },
962         { .compatible = "ti,lp8725", },
963         { }
964 };
965 MODULE_DEVICE_TABLE(of, lp872x_dt_ids);
966 
967 static const struct i2c_device_id lp872x_ids[] = {
968         {"lp8720", LP8720},
969         {"lp8725", LP8725},
970         { }
971 };
972 MODULE_DEVICE_TABLE(i2c, lp872x_ids);
973 
974 static struct i2c_driver lp872x_driver = {
975         .driver = {
976                 .name = "lp872x",
977                 .of_match_table = of_match_ptr(lp872x_dt_ids),
978         },
979         .probe = lp872x_probe,
980         .id_table = lp872x_ids,
981 };
982 
983 module_i2c_driver(lp872x_driver);
984 
985 MODULE_DESCRIPTION("TI/National Semiconductor LP872x PMU Regulator Driver");
986 MODULE_AUTHOR("Milo Kim");
987 MODULE_LICENSE("GPL");
988 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us