lp872x.c 26 KB

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