lp3972.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Regulator driver for National Semiconductors LP3972 PMIC chip
  3. *
  4. * Based on lp3971.c
  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. #include <linux/bug.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/kernel.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/lp3972.h>
  17. #include <linux/slab.h>
  18. struct lp3972 {
  19. struct device *dev;
  20. struct mutex io_lock;
  21. struct i2c_client *i2c;
  22. int num_regulators;
  23. struct regulator_dev **rdev;
  24. };
  25. /* LP3972 Control Registers */
  26. #define LP3972_SCR_REG 0x07
  27. #define LP3972_OVER1_REG 0x10
  28. #define LP3972_OVSR1_REG 0x11
  29. #define LP3972_OVER2_REG 0x12
  30. #define LP3972_OVSR2_REG 0x13
  31. #define LP3972_VCC1_REG 0x20
  32. #define LP3972_ADTV1_REG 0x23
  33. #define LP3972_ADTV2_REG 0x24
  34. #define LP3972_AVRC_REG 0x25
  35. #define LP3972_CDTC1_REG 0x26
  36. #define LP3972_CDTC2_REG 0x27
  37. #define LP3972_SDTV1_REG 0x29
  38. #define LP3972_SDTV2_REG 0x2A
  39. #define LP3972_MDTV1_REG 0x32
  40. #define LP3972_MDTV2_REG 0x33
  41. #define LP3972_L2VCR_REG 0x39
  42. #define LP3972_L34VCR_REG 0x3A
  43. #define LP3972_SCR1_REG 0x80
  44. #define LP3972_SCR2_REG 0x81
  45. #define LP3972_OEN3_REG 0x82
  46. #define LP3972_OSR3_REG 0x83
  47. #define LP3972_LOER4_REG 0x84
  48. #define LP3972_B2TV_REG 0x85
  49. #define LP3972_B3TV_REG 0x86
  50. #define LP3972_B32RC_REG 0x87
  51. #define LP3972_ISRA_REG 0x88
  52. #define LP3972_BCCR_REG 0x89
  53. #define LP3972_II1RR_REG 0x8E
  54. #define LP3972_II2RR_REG 0x8F
  55. #define LP3972_SYS_CONTROL1_REG LP3972_SCR1_REG
  56. /* System control register 1 initial value,
  57. * bits 5, 6 and 7 are EPROM programmable */
  58. #define SYS_CONTROL1_INIT_VAL 0x02
  59. #define SYS_CONTROL1_INIT_MASK 0x1F
  60. #define LP3972_VOL_CHANGE_REG LP3972_VCC1_REG
  61. #define LP3972_VOL_CHANGE_FLAG_GO 0x01
  62. #define LP3972_VOL_CHANGE_FLAG_MASK 0x03
  63. /* LDO output enable mask */
  64. #define LP3972_OEN3_L1EN BIT(0)
  65. #define LP3972_OVER2_LDO2_EN BIT(2)
  66. #define LP3972_OVER2_LDO3_EN BIT(3)
  67. #define LP3972_OVER2_LDO4_EN BIT(4)
  68. #define LP3972_OVER1_S_EN BIT(2)
  69. static const int ldo1_voltage_map[] = {
  70. 1700, 1725, 1750, 1775, 1800, 1825, 1850, 1875,
  71. 1900, 1925, 1950, 1975, 2000,
  72. };
  73. static const int ldo23_voltage_map[] = {
  74. 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500,
  75. 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300,
  76. };
  77. static const int ldo4_voltage_map[] = {
  78. 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350,
  79. 1400, 1500, 1800, 1900, 2500, 2800, 3000, 3300,
  80. };
  81. static const int ldo5_voltage_map[] = {
  82. 0, 0, 0, 0, 0, 850, 875, 900,
  83. 925, 950, 975, 1000, 1025, 1050, 1075, 1100,
  84. 1125, 1150, 1175, 1200, 1225, 1250, 1275, 1300,
  85. 1325, 1350, 1375, 1400, 1425, 1450, 1475, 1500,
  86. };
  87. static const int buck1_voltage_map[] = {
  88. 725, 750, 775, 800, 825, 850, 875, 900,
  89. 925, 950, 975, 1000, 1025, 1050, 1075, 1100,
  90. 1125, 1150, 1175, 1200, 1225, 1250, 1275, 1300,
  91. 1325, 1350, 1375, 1400, 1425, 1450, 1475, 1500,
  92. };
  93. static const int buck23_voltage_map[] = {
  94. 0, 800, 850, 900, 950, 1000, 1050, 1100,
  95. 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500,
  96. 1550, 1600, 1650, 1700, 1800, 1900, 2500, 2800,
  97. 3000, 3300,
  98. };
  99. static const int *ldo_voltage_map[] = {
  100. ldo1_voltage_map,
  101. ldo23_voltage_map,
  102. ldo23_voltage_map,
  103. ldo4_voltage_map,
  104. ldo5_voltage_map,
  105. };
  106. static const int *buck_voltage_map[] = {
  107. buck1_voltage_map,
  108. buck23_voltage_map,
  109. buck23_voltage_map,
  110. };
  111. static const int ldo_output_enable_mask[] = {
  112. LP3972_OEN3_L1EN,
  113. LP3972_OVER2_LDO2_EN,
  114. LP3972_OVER2_LDO3_EN,
  115. LP3972_OVER2_LDO4_EN,
  116. LP3972_OVER1_S_EN,
  117. };
  118. static const int ldo_output_enable_addr[] = {
  119. LP3972_OEN3_REG,
  120. LP3972_OVER2_REG,
  121. LP3972_OVER2_REG,
  122. LP3972_OVER2_REG,
  123. LP3972_OVER1_REG,
  124. };
  125. static const int ldo_vol_ctl_addr[] = {
  126. LP3972_MDTV1_REG,
  127. LP3972_L2VCR_REG,
  128. LP3972_L34VCR_REG,
  129. LP3972_L34VCR_REG,
  130. LP3972_SDTV1_REG,
  131. };
  132. static const int buck_vol_enable_addr[] = {
  133. LP3972_OVER1_REG,
  134. LP3972_OEN3_REG,
  135. LP3972_OEN3_REG,
  136. };
  137. static const int buck_base_addr[] = {
  138. LP3972_ADTV1_REG,
  139. LP3972_B2TV_REG,
  140. LP3972_B3TV_REG,
  141. };
  142. #define LP3972_LDO_VOL_VALUE_MAP(x) (ldo_voltage_map[x])
  143. #define LP3972_LDO_OUTPUT_ENABLE_MASK(x) (ldo_output_enable_mask[x])
  144. #define LP3972_LDO_OUTPUT_ENABLE_REG(x) (ldo_output_enable_addr[x])
  145. /* LDO voltage control registers shift:
  146. LP3972_LDO1 -> 0, LP3972_LDO2 -> 4
  147. LP3972_LDO3 -> 0, LP3972_LDO4 -> 4
  148. LP3972_LDO5 -> 0
  149. */
  150. #define LP3972_LDO_VOL_CONTR_SHIFT(x) (((x) & 1) << 2)
  151. #define LP3972_LDO_VOL_CONTR_REG(x) (ldo_vol_ctl_addr[x])
  152. #define LP3972_LDO_VOL_CHANGE_SHIFT(x) ((x) ? 4 : 6)
  153. #define LP3972_LDO_VOL_MASK(x) (((x) % 4) ? 0x0f : 0x1f)
  154. #define LP3972_LDO_VOL_MIN_IDX(x) (((x) == 4) ? 0x05 : 0x00)
  155. #define LP3972_LDO_VOL_MAX_IDX(x) ((x) ? (((x) == 4) ? 0x1f : 0x0f) : 0x0c)
  156. #define LP3972_BUCK_VOL_VALUE_MAP(x) (buck_voltage_map[x])
  157. #define LP3972_BUCK_VOL_ENABLE_REG(x) (buck_vol_enable_addr[x])
  158. #define LP3972_BUCK_VOL1_REG(x) (buck_base_addr[x])
  159. #define LP3972_BUCK_VOL_MASK 0x1f
  160. #define LP3972_BUCK_VOL_MIN_IDX(x) ((x) ? 0x01 : 0x00)
  161. #define LP3972_BUCK_VOL_MAX_IDX(x) ((x) ? 0x19 : 0x1f)
  162. static int lp3972_i2c_read(struct i2c_client *i2c, char reg, int count,
  163. u16 *dest)
  164. {
  165. int ret;
  166. if (count != 1)
  167. return -EIO;
  168. ret = i2c_smbus_read_byte_data(i2c, reg);
  169. if (ret < 0)
  170. return ret;
  171. *dest = ret;
  172. return 0;
  173. }
  174. static int lp3972_i2c_write(struct i2c_client *i2c, char reg, int count,
  175. const u16 *src)
  176. {
  177. if (count != 1)
  178. return -EIO;
  179. return i2c_smbus_write_byte_data(i2c, reg, *src);
  180. }
  181. static u8 lp3972_reg_read(struct lp3972 *lp3972, u8 reg)
  182. {
  183. u16 val = 0;
  184. mutex_lock(&lp3972->io_lock);
  185. lp3972_i2c_read(lp3972->i2c, reg, 1, &val);
  186. dev_dbg(lp3972->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
  187. (unsigned)val & 0xff);
  188. mutex_unlock(&lp3972->io_lock);
  189. return val & 0xff;
  190. }
  191. static int lp3972_set_bits(struct lp3972 *lp3972, u8 reg, u16 mask, u16 val)
  192. {
  193. u16 tmp;
  194. int ret;
  195. mutex_lock(&lp3972->io_lock);
  196. ret = lp3972_i2c_read(lp3972->i2c, reg, 1, &tmp);
  197. tmp = (tmp & ~mask) | val;
  198. if (ret == 0) {
  199. ret = lp3972_i2c_write(lp3972->i2c, reg, 1, &tmp);
  200. dev_dbg(lp3972->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
  201. (unsigned)val & 0xff);
  202. }
  203. mutex_unlock(&lp3972->io_lock);
  204. return ret;
  205. }
  206. static int lp3972_ldo_list_voltage(struct regulator_dev *dev, unsigned index)
  207. {
  208. int ldo = rdev_get_id(dev) - LP3972_LDO1;
  209. return 1000 * LP3972_LDO_VOL_VALUE_MAP(ldo)[index];
  210. }
  211. static int lp3972_ldo_is_enabled(struct regulator_dev *dev)
  212. {
  213. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  214. int ldo = rdev_get_id(dev) - LP3972_LDO1;
  215. u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
  216. u16 val;
  217. val = lp3972_reg_read(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo));
  218. return !!(val & mask);
  219. }
  220. static int lp3972_ldo_enable(struct regulator_dev *dev)
  221. {
  222. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  223. int ldo = rdev_get_id(dev) - LP3972_LDO1;
  224. u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
  225. return lp3972_set_bits(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo),
  226. mask, mask);
  227. }
  228. static int lp3972_ldo_disable(struct regulator_dev *dev)
  229. {
  230. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  231. int ldo = rdev_get_id(dev) - LP3972_LDO1;
  232. u16 mask = LP3972_LDO_OUTPUT_ENABLE_MASK(ldo);
  233. return lp3972_set_bits(lp3972, LP3972_LDO_OUTPUT_ENABLE_REG(ldo),
  234. mask, 0);
  235. }
  236. static int lp3972_ldo_get_voltage(struct regulator_dev *dev)
  237. {
  238. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  239. int ldo = rdev_get_id(dev) - LP3972_LDO1;
  240. u16 mask = LP3972_LDO_VOL_MASK(ldo);
  241. u16 val, reg;
  242. reg = lp3972_reg_read(lp3972, LP3972_LDO_VOL_CONTR_REG(ldo));
  243. val = (reg >> LP3972_LDO_VOL_CONTR_SHIFT(ldo)) & mask;
  244. return 1000 * LP3972_LDO_VOL_VALUE_MAP(ldo)[val];
  245. }
  246. static int lp3972_ldo_set_voltage(struct regulator_dev *dev,
  247. int min_uV, int max_uV,
  248. unsigned int *selector)
  249. {
  250. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  251. int ldo = rdev_get_id(dev) - LP3972_LDO1;
  252. int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
  253. const int *vol_map = LP3972_LDO_VOL_VALUE_MAP(ldo);
  254. u16 val;
  255. int shift, ret;
  256. if (min_vol < vol_map[LP3972_LDO_VOL_MIN_IDX(ldo)] ||
  257. min_vol > vol_map[LP3972_LDO_VOL_MAX_IDX(ldo)])
  258. return -EINVAL;
  259. for (val = LP3972_LDO_VOL_MIN_IDX(ldo);
  260. val <= LP3972_LDO_VOL_MAX_IDX(ldo); val++)
  261. if (vol_map[val] >= min_vol)
  262. break;
  263. if (val > LP3972_LDO_VOL_MAX_IDX(ldo) || vol_map[val] > max_vol)
  264. return -EINVAL;
  265. *selector = val;
  266. shift = LP3972_LDO_VOL_CONTR_SHIFT(ldo);
  267. ret = lp3972_set_bits(lp3972, LP3972_LDO_VOL_CONTR_REG(ldo),
  268. LP3972_LDO_VOL_MASK(ldo) << shift, val << shift);
  269. if (ret)
  270. return ret;
  271. /*
  272. * LDO1 and LDO5 support voltage control by either target voltage1
  273. * or target voltage2 register.
  274. * We use target voltage1 register for LDO1 and LDO5 in this driver.
  275. * We need to update voltage change control register(0x20) to enable
  276. * LDO1 and LDO5 to change to their programmed target values.
  277. */
  278. switch (ldo) {
  279. case LP3972_LDO1:
  280. case LP3972_LDO5:
  281. shift = LP3972_LDO_VOL_CHANGE_SHIFT(ldo);
  282. ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
  283. LP3972_VOL_CHANGE_FLAG_MASK << shift,
  284. LP3972_VOL_CHANGE_FLAG_GO << shift);
  285. if (ret)
  286. return ret;
  287. ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
  288. LP3972_VOL_CHANGE_FLAG_MASK << shift, 0);
  289. break;
  290. }
  291. return ret;
  292. }
  293. static struct regulator_ops lp3972_ldo_ops = {
  294. .list_voltage = lp3972_ldo_list_voltage,
  295. .is_enabled = lp3972_ldo_is_enabled,
  296. .enable = lp3972_ldo_enable,
  297. .disable = lp3972_ldo_disable,
  298. .get_voltage = lp3972_ldo_get_voltage,
  299. .set_voltage = lp3972_ldo_set_voltage,
  300. };
  301. static int lp3972_dcdc_list_voltage(struct regulator_dev *dev, unsigned index)
  302. {
  303. int buck = rdev_get_id(dev) - LP3972_DCDC1;
  304. return 1000 * buck_voltage_map[buck][index];
  305. }
  306. static int lp3972_dcdc_is_enabled(struct regulator_dev *dev)
  307. {
  308. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  309. int buck = rdev_get_id(dev) - LP3972_DCDC1;
  310. u16 mask = 1 << (buck * 2);
  311. u16 val;
  312. val = lp3972_reg_read(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck));
  313. return !!(val & mask);
  314. }
  315. static int lp3972_dcdc_enable(struct regulator_dev *dev)
  316. {
  317. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  318. int buck = rdev_get_id(dev) - LP3972_DCDC1;
  319. u16 mask = 1 << (buck * 2);
  320. u16 val;
  321. val = lp3972_set_bits(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck),
  322. mask, mask);
  323. return val;
  324. }
  325. static int lp3972_dcdc_disable(struct regulator_dev *dev)
  326. {
  327. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  328. int buck = rdev_get_id(dev) - LP3972_DCDC1;
  329. u16 mask = 1 << (buck * 2);
  330. u16 val;
  331. val = lp3972_set_bits(lp3972, LP3972_BUCK_VOL_ENABLE_REG(buck),
  332. mask, 0);
  333. return val;
  334. }
  335. static int lp3972_dcdc_get_voltage(struct regulator_dev *dev)
  336. {
  337. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  338. int buck = rdev_get_id(dev) - LP3972_DCDC1;
  339. u16 reg;
  340. int val;
  341. reg = lp3972_reg_read(lp3972, LP3972_BUCK_VOL1_REG(buck));
  342. reg &= LP3972_BUCK_VOL_MASK;
  343. if (reg <= LP3972_BUCK_VOL_MAX_IDX(buck))
  344. val = 1000 * buck_voltage_map[buck][reg];
  345. else {
  346. val = 0;
  347. dev_warn(&dev->dev, "chip reported incorrect voltage value."
  348. " reg = %d\n", reg);
  349. }
  350. return val;
  351. }
  352. static int lp3972_dcdc_set_voltage(struct regulator_dev *dev,
  353. int min_uV, int max_uV,
  354. unsigned int *selector)
  355. {
  356. struct lp3972 *lp3972 = rdev_get_drvdata(dev);
  357. int buck = rdev_get_id(dev) - LP3972_DCDC1;
  358. int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
  359. const int *vol_map = buck_voltage_map[buck];
  360. u16 val;
  361. int ret;
  362. if (min_vol < vol_map[LP3972_BUCK_VOL_MIN_IDX(buck)] ||
  363. min_vol > vol_map[LP3972_BUCK_VOL_MAX_IDX(buck)])
  364. return -EINVAL;
  365. for (val = LP3972_BUCK_VOL_MIN_IDX(buck);
  366. val <= LP3972_BUCK_VOL_MAX_IDX(buck); val++)
  367. if (vol_map[val] >= min_vol)
  368. break;
  369. if (val > LP3972_BUCK_VOL_MAX_IDX(buck) ||
  370. vol_map[val] > max_vol)
  371. return -EINVAL;
  372. *selector = val;
  373. ret = lp3972_set_bits(lp3972, LP3972_BUCK_VOL1_REG(buck),
  374. LP3972_BUCK_VOL_MASK, val);
  375. if (ret)
  376. return ret;
  377. if (buck != 0)
  378. return ret;
  379. ret = lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
  380. LP3972_VOL_CHANGE_FLAG_MASK, LP3972_VOL_CHANGE_FLAG_GO);
  381. if (ret)
  382. return ret;
  383. return lp3972_set_bits(lp3972, LP3972_VOL_CHANGE_REG,
  384. LP3972_VOL_CHANGE_FLAG_MASK, 0);
  385. }
  386. static struct regulator_ops lp3972_dcdc_ops = {
  387. .list_voltage = lp3972_dcdc_list_voltage,
  388. .is_enabled = lp3972_dcdc_is_enabled,
  389. .enable = lp3972_dcdc_enable,
  390. .disable = lp3972_dcdc_disable,
  391. .get_voltage = lp3972_dcdc_get_voltage,
  392. .set_voltage = lp3972_dcdc_set_voltage,
  393. };
  394. static struct regulator_desc regulators[] = {
  395. {
  396. .name = "LDO1",
  397. .id = LP3972_LDO1,
  398. .ops = &lp3972_ldo_ops,
  399. .n_voltages = ARRAY_SIZE(ldo1_voltage_map),
  400. .type = REGULATOR_VOLTAGE,
  401. .owner = THIS_MODULE,
  402. },
  403. {
  404. .name = "LDO2",
  405. .id = LP3972_LDO2,
  406. .ops = &lp3972_ldo_ops,
  407. .n_voltages = ARRAY_SIZE(ldo23_voltage_map),
  408. .type = REGULATOR_VOLTAGE,
  409. .owner = THIS_MODULE,
  410. },
  411. {
  412. .name = "LDO3",
  413. .id = LP3972_LDO3,
  414. .ops = &lp3972_ldo_ops,
  415. .n_voltages = ARRAY_SIZE(ldo23_voltage_map),
  416. .type = REGULATOR_VOLTAGE,
  417. .owner = THIS_MODULE,
  418. },
  419. {
  420. .name = "LDO4",
  421. .id = LP3972_LDO4,
  422. .ops = &lp3972_ldo_ops,
  423. .n_voltages = ARRAY_SIZE(ldo4_voltage_map),
  424. .type = REGULATOR_VOLTAGE,
  425. .owner = THIS_MODULE,
  426. },
  427. {
  428. .name = "LDO5",
  429. .id = LP3972_LDO5,
  430. .ops = &lp3972_ldo_ops,
  431. .n_voltages = ARRAY_SIZE(ldo5_voltage_map),
  432. .type = REGULATOR_VOLTAGE,
  433. .owner = THIS_MODULE,
  434. },
  435. {
  436. .name = "DCDC1",
  437. .id = LP3972_DCDC1,
  438. .ops = &lp3972_dcdc_ops,
  439. .n_voltages = ARRAY_SIZE(buck1_voltage_map),
  440. .type = REGULATOR_VOLTAGE,
  441. .owner = THIS_MODULE,
  442. },
  443. {
  444. .name = "DCDC2",
  445. .id = LP3972_DCDC2,
  446. .ops = &lp3972_dcdc_ops,
  447. .n_voltages = ARRAY_SIZE(buck23_voltage_map),
  448. .type = REGULATOR_VOLTAGE,
  449. .owner = THIS_MODULE,
  450. },
  451. {
  452. .name = "DCDC3",
  453. .id = LP3972_DCDC3,
  454. .ops = &lp3972_dcdc_ops,
  455. .n_voltages = ARRAY_SIZE(buck23_voltage_map),
  456. .type = REGULATOR_VOLTAGE,
  457. .owner = THIS_MODULE,
  458. },
  459. };
  460. static int __devinit setup_regulators(struct lp3972 *lp3972,
  461. struct lp3972_platform_data *pdata)
  462. {
  463. int i, err;
  464. lp3972->num_regulators = pdata->num_regulators;
  465. lp3972->rdev = kcalloc(pdata->num_regulators,
  466. sizeof(struct regulator_dev *), GFP_KERNEL);
  467. if (!lp3972->rdev) {
  468. err = -ENOMEM;
  469. goto err_nomem;
  470. }
  471. /* Instantiate the regulators */
  472. for (i = 0; i < pdata->num_regulators; i++) {
  473. struct lp3972_regulator_subdev *reg = &pdata->regulators[i];
  474. lp3972->rdev[i] = regulator_register(&regulators[reg->id],
  475. lp3972->dev, reg->initdata, lp3972);
  476. if (IS_ERR(lp3972->rdev[i])) {
  477. err = PTR_ERR(lp3972->rdev[i]);
  478. dev_err(lp3972->dev, "regulator init failed: %d\n",
  479. err);
  480. goto error;
  481. }
  482. }
  483. return 0;
  484. error:
  485. while (--i >= 0)
  486. regulator_unregister(lp3972->rdev[i]);
  487. kfree(lp3972->rdev);
  488. lp3972->rdev = NULL;
  489. err_nomem:
  490. return err;
  491. }
  492. static int __devinit lp3972_i2c_probe(struct i2c_client *i2c,
  493. const struct i2c_device_id *id)
  494. {
  495. struct lp3972 *lp3972;
  496. struct lp3972_platform_data *pdata = i2c->dev.platform_data;
  497. int ret;
  498. u16 val;
  499. if (!pdata) {
  500. dev_dbg(&i2c->dev, "No platform init data supplied\n");
  501. return -ENODEV;
  502. }
  503. lp3972 = kzalloc(sizeof(struct lp3972), GFP_KERNEL);
  504. if (!lp3972)
  505. return -ENOMEM;
  506. lp3972->i2c = i2c;
  507. lp3972->dev = &i2c->dev;
  508. mutex_init(&lp3972->io_lock);
  509. /* Detect LP3972 */
  510. ret = lp3972_i2c_read(i2c, LP3972_SYS_CONTROL1_REG, 1, &val);
  511. if (ret == 0 &&
  512. (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL) {
  513. ret = -ENODEV;
  514. dev_err(&i2c->dev, "chip reported: val = 0x%x\n", val);
  515. }
  516. if (ret < 0) {
  517. dev_err(&i2c->dev, "failed to detect device. ret = %d\n", ret);
  518. goto err_detect;
  519. }
  520. ret = setup_regulators(lp3972, pdata);
  521. if (ret < 0)
  522. goto err_detect;
  523. i2c_set_clientdata(i2c, lp3972);
  524. return 0;
  525. err_detect:
  526. kfree(lp3972);
  527. return ret;
  528. }
  529. static int __devexit lp3972_i2c_remove(struct i2c_client *i2c)
  530. {
  531. struct lp3972 *lp3972 = i2c_get_clientdata(i2c);
  532. int i;
  533. for (i = 0; i < lp3972->num_regulators; i++)
  534. regulator_unregister(lp3972->rdev[i]);
  535. kfree(lp3972->rdev);
  536. kfree(lp3972);
  537. return 0;
  538. }
  539. static const struct i2c_device_id lp3972_i2c_id[] = {
  540. { "lp3972", 0 },
  541. { }
  542. };
  543. MODULE_DEVICE_TABLE(i2c, lp3972_i2c_id);
  544. static struct i2c_driver lp3972_i2c_driver = {
  545. .driver = {
  546. .name = "lp3972",
  547. .owner = THIS_MODULE,
  548. },
  549. .probe = lp3972_i2c_probe,
  550. .remove = __devexit_p(lp3972_i2c_remove),
  551. .id_table = lp3972_i2c_id,
  552. };
  553. static int __init lp3972_module_init(void)
  554. {
  555. return i2c_add_driver(&lp3972_i2c_driver);
  556. }
  557. subsys_initcall(lp3972_module_init);
  558. static void __exit lp3972_module_exit(void)
  559. {
  560. i2c_del_driver(&lp3972_i2c_driver);
  561. }
  562. module_exit(lp3972_module_exit);
  563. MODULE_LICENSE("GPL");
  564. MODULE_AUTHOR("Axel Lin <axel.lin@gmail.com>");
  565. MODULE_DESCRIPTION("LP3972 PMIC driver");