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