wm831x-ldo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. * wm831x-ldo.c -- LDO driver for the WM831x series
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/bitops.h>
  17. #include <linux/err.h>
  18. #include <linux/i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/slab.h>
  22. #include <linux/mfd/wm831x/core.h>
  23. #include <linux/mfd/wm831x/regulator.h>
  24. #include <linux/mfd/wm831x/pdata.h>
  25. #define WM831X_LDO_MAX_NAME 6
  26. #define WM831X_LDO_CONTROL 0
  27. #define WM831X_LDO_ON_CONTROL 1
  28. #define WM831X_LDO_SLEEP_CONTROL 2
  29. #define WM831X_ALIVE_LDO_ON_CONTROL 0
  30. #define WM831X_ALIVE_LDO_SLEEP_CONTROL 1
  31. struct wm831x_ldo {
  32. char name[WM831X_LDO_MAX_NAME];
  33. struct regulator_desc desc;
  34. int base;
  35. struct wm831x *wm831x;
  36. struct regulator_dev *regulator;
  37. };
  38. /*
  39. * Shared
  40. */
  41. static int wm831x_ldo_is_enabled(struct regulator_dev *rdev)
  42. {
  43. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  44. struct wm831x *wm831x = ldo->wm831x;
  45. int mask = 1 << rdev_get_id(rdev);
  46. int reg;
  47. reg = wm831x_reg_read(wm831x, WM831X_LDO_ENABLE);
  48. if (reg < 0)
  49. return reg;
  50. if (reg & mask)
  51. return 1;
  52. else
  53. return 0;
  54. }
  55. static int wm831x_ldo_enable(struct regulator_dev *rdev)
  56. {
  57. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  58. struct wm831x *wm831x = ldo->wm831x;
  59. int mask = 1 << rdev_get_id(rdev);
  60. return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, mask);
  61. }
  62. static int wm831x_ldo_disable(struct regulator_dev *rdev)
  63. {
  64. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  65. struct wm831x *wm831x = ldo->wm831x;
  66. int mask = 1 << rdev_get_id(rdev);
  67. return wm831x_set_bits(wm831x, WM831X_LDO_ENABLE, mask, 0);
  68. }
  69. static irqreturn_t wm831x_ldo_uv_irq(int irq, void *data)
  70. {
  71. struct wm831x_ldo *ldo = data;
  72. regulator_notifier_call_chain(ldo->regulator,
  73. REGULATOR_EVENT_UNDER_VOLTAGE,
  74. NULL);
  75. return IRQ_HANDLED;
  76. }
  77. /*
  78. * General purpose LDOs
  79. */
  80. #define WM831X_GP_LDO_SELECTOR_LOW 0xe
  81. #define WM831X_GP_LDO_MAX_SELECTOR 0x1f
  82. static int wm831x_gp_ldo_list_voltage(struct regulator_dev *rdev,
  83. unsigned int selector)
  84. {
  85. /* 0.9-1.6V in 50mV steps */
  86. if (selector <= WM831X_GP_LDO_SELECTOR_LOW)
  87. return 900000 + (selector * 50000);
  88. /* 1.7-3.3V in 50mV steps */
  89. if (selector <= WM831X_GP_LDO_MAX_SELECTOR)
  90. return 1600000 + ((selector - WM831X_GP_LDO_SELECTOR_LOW)
  91. * 100000);
  92. return -EINVAL;
  93. }
  94. static int wm831x_gp_ldo_set_voltage_int(struct regulator_dev *rdev, int reg,
  95. int min_uV, int max_uV,
  96. unsigned *selector)
  97. {
  98. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  99. struct wm831x *wm831x = ldo->wm831x;
  100. int vsel, ret;
  101. if (min_uV < 900000)
  102. vsel = 0;
  103. else if (min_uV < 1700000)
  104. vsel = ((min_uV - 900000) / 50000);
  105. else
  106. vsel = ((min_uV - 1700000) / 100000)
  107. + WM831X_GP_LDO_SELECTOR_LOW + 1;
  108. ret = wm831x_gp_ldo_list_voltage(rdev, vsel);
  109. if (ret < 0)
  110. return ret;
  111. if (ret < min_uV || ret > max_uV)
  112. return -EINVAL;
  113. *selector = vsel;
  114. return wm831x_set_bits(wm831x, reg, WM831X_LDO1_ON_VSEL_MASK, vsel);
  115. }
  116. static int wm831x_gp_ldo_set_voltage(struct regulator_dev *rdev,
  117. int min_uV, int max_uV,
  118. unsigned *selector)
  119. {
  120. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  121. int reg = ldo->base + WM831X_LDO_ON_CONTROL;
  122. return wm831x_gp_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
  123. selector);
  124. }
  125. static int wm831x_gp_ldo_set_suspend_voltage(struct regulator_dev *rdev,
  126. int uV)
  127. {
  128. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  129. int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
  130. unsigned int selector;
  131. return wm831x_gp_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
  132. }
  133. static int wm831x_gp_ldo_get_voltage_sel(struct regulator_dev *rdev)
  134. {
  135. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  136. struct wm831x *wm831x = ldo->wm831x;
  137. int reg = ldo->base + WM831X_LDO_ON_CONTROL;
  138. int ret;
  139. ret = wm831x_reg_read(wm831x, reg);
  140. if (ret < 0)
  141. return ret;
  142. ret &= WM831X_LDO1_ON_VSEL_MASK;
  143. return ret;
  144. }
  145. static unsigned int wm831x_gp_ldo_get_mode(struct regulator_dev *rdev)
  146. {
  147. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  148. struct wm831x *wm831x = ldo->wm831x;
  149. int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
  150. int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
  151. int ret;
  152. ret = wm831x_reg_read(wm831x, on_reg);
  153. if (ret < 0)
  154. return ret;
  155. if (!(ret & WM831X_LDO1_ON_MODE))
  156. return REGULATOR_MODE_NORMAL;
  157. ret = wm831x_reg_read(wm831x, ctrl_reg);
  158. if (ret < 0)
  159. return ret;
  160. if (ret & WM831X_LDO1_LP_MODE)
  161. return REGULATOR_MODE_STANDBY;
  162. else
  163. return REGULATOR_MODE_IDLE;
  164. }
  165. static int wm831x_gp_ldo_set_mode(struct regulator_dev *rdev,
  166. unsigned int mode)
  167. {
  168. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  169. struct wm831x *wm831x = ldo->wm831x;
  170. int ctrl_reg = ldo->base + WM831X_LDO_CONTROL;
  171. int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
  172. int ret;
  173. switch (mode) {
  174. case REGULATOR_MODE_NORMAL:
  175. ret = wm831x_set_bits(wm831x, on_reg,
  176. WM831X_LDO1_ON_MODE, 0);
  177. if (ret < 0)
  178. return ret;
  179. break;
  180. case REGULATOR_MODE_IDLE:
  181. ret = wm831x_set_bits(wm831x, ctrl_reg,
  182. WM831X_LDO1_LP_MODE, 0);
  183. if (ret < 0)
  184. return ret;
  185. ret = wm831x_set_bits(wm831x, on_reg,
  186. WM831X_LDO1_ON_MODE,
  187. WM831X_LDO1_ON_MODE);
  188. if (ret < 0)
  189. return ret;
  190. break;
  191. case REGULATOR_MODE_STANDBY:
  192. ret = wm831x_set_bits(wm831x, ctrl_reg,
  193. WM831X_LDO1_LP_MODE,
  194. WM831X_LDO1_LP_MODE);
  195. if (ret < 0)
  196. return ret;
  197. ret = wm831x_set_bits(wm831x, on_reg,
  198. WM831X_LDO1_ON_MODE,
  199. WM831X_LDO1_ON_MODE);
  200. if (ret < 0)
  201. return ret;
  202. break;
  203. default:
  204. return -EINVAL;
  205. }
  206. return 0;
  207. }
  208. static int wm831x_gp_ldo_get_status(struct regulator_dev *rdev)
  209. {
  210. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  211. struct wm831x *wm831x = ldo->wm831x;
  212. int mask = 1 << rdev_get_id(rdev);
  213. int ret;
  214. /* Is the regulator on? */
  215. ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
  216. if (ret < 0)
  217. return ret;
  218. if (!(ret & mask))
  219. return REGULATOR_STATUS_OFF;
  220. /* Is it reporting under voltage? */
  221. ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
  222. if (ret & mask)
  223. return REGULATOR_STATUS_ERROR;
  224. ret = wm831x_gp_ldo_get_mode(rdev);
  225. if (ret < 0)
  226. return ret;
  227. else
  228. return regulator_mode_to_status(ret);
  229. }
  230. static unsigned int wm831x_gp_ldo_get_optimum_mode(struct regulator_dev *rdev,
  231. int input_uV,
  232. int output_uV, int load_uA)
  233. {
  234. if (load_uA < 20000)
  235. return REGULATOR_MODE_STANDBY;
  236. if (load_uA < 50000)
  237. return REGULATOR_MODE_IDLE;
  238. return REGULATOR_MODE_NORMAL;
  239. }
  240. static struct regulator_ops wm831x_gp_ldo_ops = {
  241. .list_voltage = wm831x_gp_ldo_list_voltage,
  242. .get_voltage_sel = wm831x_gp_ldo_get_voltage_sel,
  243. .set_voltage = wm831x_gp_ldo_set_voltage,
  244. .set_suspend_voltage = wm831x_gp_ldo_set_suspend_voltage,
  245. .get_mode = wm831x_gp_ldo_get_mode,
  246. .set_mode = wm831x_gp_ldo_set_mode,
  247. .get_status = wm831x_gp_ldo_get_status,
  248. .get_optimum_mode = wm831x_gp_ldo_get_optimum_mode,
  249. .is_enabled = wm831x_ldo_is_enabled,
  250. .enable = wm831x_ldo_enable,
  251. .disable = wm831x_ldo_disable,
  252. };
  253. static __devinit int wm831x_gp_ldo_probe(struct platform_device *pdev)
  254. {
  255. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  256. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  257. int id;
  258. struct wm831x_ldo *ldo;
  259. struct resource *res;
  260. int ret, irq;
  261. if (pdata && pdata->wm831x_num)
  262. id = (pdata->wm831x_num * 10) + 1;
  263. else
  264. id = 0;
  265. id = pdev->id - id;
  266. dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
  267. if (pdata == NULL || pdata->ldo[id] == NULL)
  268. return -ENODEV;
  269. ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
  270. if (ldo == NULL) {
  271. dev_err(&pdev->dev, "Unable to allocate private data\n");
  272. return -ENOMEM;
  273. }
  274. ldo->wm831x = wm831x;
  275. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  276. if (res == NULL) {
  277. dev_err(&pdev->dev, "No I/O resource\n");
  278. ret = -EINVAL;
  279. goto err;
  280. }
  281. ldo->base = res->start;
  282. snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
  283. ldo->desc.name = ldo->name;
  284. ldo->desc.id = id;
  285. ldo->desc.type = REGULATOR_VOLTAGE;
  286. ldo->desc.n_voltages = WM831X_GP_LDO_MAX_SELECTOR + 1;
  287. ldo->desc.ops = &wm831x_gp_ldo_ops;
  288. ldo->desc.owner = THIS_MODULE;
  289. ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
  290. pdata->ldo[id], ldo, NULL);
  291. if (IS_ERR(ldo->regulator)) {
  292. ret = PTR_ERR(ldo->regulator);
  293. dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
  294. id + 1, ret);
  295. goto err;
  296. }
  297. irq = platform_get_irq_byname(pdev, "UV");
  298. ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
  299. IRQF_TRIGGER_RISING, ldo->name,
  300. ldo);
  301. if (ret != 0) {
  302. dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
  303. irq, ret);
  304. goto err_regulator;
  305. }
  306. platform_set_drvdata(pdev, ldo);
  307. return 0;
  308. err_regulator:
  309. regulator_unregister(ldo->regulator);
  310. err:
  311. return ret;
  312. }
  313. static __devexit int wm831x_gp_ldo_remove(struct platform_device *pdev)
  314. {
  315. struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
  316. platform_set_drvdata(pdev, NULL);
  317. free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
  318. regulator_unregister(ldo->regulator);
  319. return 0;
  320. }
  321. static struct platform_driver wm831x_gp_ldo_driver = {
  322. .probe = wm831x_gp_ldo_probe,
  323. .remove = __devexit_p(wm831x_gp_ldo_remove),
  324. .driver = {
  325. .name = "wm831x-ldo",
  326. .owner = THIS_MODULE,
  327. },
  328. };
  329. /*
  330. * Analogue LDOs
  331. */
  332. #define WM831X_ALDO_SELECTOR_LOW 0xc
  333. #define WM831X_ALDO_MAX_SELECTOR 0x1f
  334. static int wm831x_aldo_list_voltage(struct regulator_dev *rdev,
  335. unsigned int selector)
  336. {
  337. /* 1-1.6V in 50mV steps */
  338. if (selector <= WM831X_ALDO_SELECTOR_LOW)
  339. return 1000000 + (selector * 50000);
  340. /* 1.7-3.5V in 50mV steps */
  341. if (selector <= WM831X_ALDO_MAX_SELECTOR)
  342. return 1600000 + ((selector - WM831X_ALDO_SELECTOR_LOW)
  343. * 100000);
  344. return -EINVAL;
  345. }
  346. static int wm831x_aldo_set_voltage_int(struct regulator_dev *rdev, int reg,
  347. int min_uV, int max_uV,
  348. unsigned *selector)
  349. {
  350. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  351. struct wm831x *wm831x = ldo->wm831x;
  352. int vsel, ret;
  353. if (min_uV < 1000000)
  354. vsel = 0;
  355. else if (min_uV < 1700000)
  356. vsel = ((min_uV - 1000000) / 50000);
  357. else
  358. vsel = ((min_uV - 1700000) / 100000)
  359. + WM831X_ALDO_SELECTOR_LOW + 1;
  360. ret = wm831x_aldo_list_voltage(rdev, vsel);
  361. if (ret < 0)
  362. return ret;
  363. if (ret < min_uV || ret > max_uV)
  364. return -EINVAL;
  365. *selector = vsel;
  366. return wm831x_set_bits(wm831x, reg, WM831X_LDO7_ON_VSEL_MASK, vsel);
  367. }
  368. static int wm831x_aldo_set_voltage(struct regulator_dev *rdev,
  369. int min_uV, int max_uV, unsigned *selector)
  370. {
  371. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  372. int reg = ldo->base + WM831X_LDO_ON_CONTROL;
  373. return wm831x_aldo_set_voltage_int(rdev, reg, min_uV, max_uV,
  374. selector);
  375. }
  376. static int wm831x_aldo_set_suspend_voltage(struct regulator_dev *rdev,
  377. int uV)
  378. {
  379. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  380. int reg = ldo->base + WM831X_LDO_SLEEP_CONTROL;
  381. unsigned int selector;
  382. return wm831x_aldo_set_voltage_int(rdev, reg, uV, uV, &selector);
  383. }
  384. static int wm831x_aldo_get_voltage_sel(struct regulator_dev *rdev)
  385. {
  386. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  387. struct wm831x *wm831x = ldo->wm831x;
  388. int reg = ldo->base + WM831X_LDO_ON_CONTROL;
  389. int ret;
  390. ret = wm831x_reg_read(wm831x, reg);
  391. if (ret < 0)
  392. return ret;
  393. ret &= WM831X_LDO7_ON_VSEL_MASK;
  394. return ret;
  395. }
  396. static unsigned int wm831x_aldo_get_mode(struct regulator_dev *rdev)
  397. {
  398. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  399. struct wm831x *wm831x = ldo->wm831x;
  400. int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
  401. int ret;
  402. ret = wm831x_reg_read(wm831x, on_reg);
  403. if (ret < 0)
  404. return 0;
  405. if (ret & WM831X_LDO7_ON_MODE)
  406. return REGULATOR_MODE_IDLE;
  407. else
  408. return REGULATOR_MODE_NORMAL;
  409. }
  410. static int wm831x_aldo_set_mode(struct regulator_dev *rdev,
  411. unsigned int mode)
  412. {
  413. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  414. struct wm831x *wm831x = ldo->wm831x;
  415. int on_reg = ldo->base + WM831X_LDO_ON_CONTROL;
  416. int ret;
  417. switch (mode) {
  418. case REGULATOR_MODE_NORMAL:
  419. ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE, 0);
  420. if (ret < 0)
  421. return ret;
  422. break;
  423. case REGULATOR_MODE_IDLE:
  424. ret = wm831x_set_bits(wm831x, on_reg, WM831X_LDO7_ON_MODE,
  425. WM831X_LDO7_ON_MODE);
  426. if (ret < 0)
  427. return ret;
  428. break;
  429. default:
  430. return -EINVAL;
  431. }
  432. return 0;
  433. }
  434. static int wm831x_aldo_get_status(struct regulator_dev *rdev)
  435. {
  436. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  437. struct wm831x *wm831x = ldo->wm831x;
  438. int mask = 1 << rdev_get_id(rdev);
  439. int ret;
  440. /* Is the regulator on? */
  441. ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
  442. if (ret < 0)
  443. return ret;
  444. if (!(ret & mask))
  445. return REGULATOR_STATUS_OFF;
  446. /* Is it reporting under voltage? */
  447. ret = wm831x_reg_read(wm831x, WM831X_LDO_UV_STATUS);
  448. if (ret & mask)
  449. return REGULATOR_STATUS_ERROR;
  450. ret = wm831x_aldo_get_mode(rdev);
  451. if (ret < 0)
  452. return ret;
  453. else
  454. return regulator_mode_to_status(ret);
  455. }
  456. static struct regulator_ops wm831x_aldo_ops = {
  457. .list_voltage = wm831x_aldo_list_voltage,
  458. .get_voltage_sel = wm831x_aldo_get_voltage_sel,
  459. .set_voltage = wm831x_aldo_set_voltage,
  460. .set_suspend_voltage = wm831x_aldo_set_suspend_voltage,
  461. .get_mode = wm831x_aldo_get_mode,
  462. .set_mode = wm831x_aldo_set_mode,
  463. .get_status = wm831x_aldo_get_status,
  464. .is_enabled = wm831x_ldo_is_enabled,
  465. .enable = wm831x_ldo_enable,
  466. .disable = wm831x_ldo_disable,
  467. };
  468. static __devinit int wm831x_aldo_probe(struct platform_device *pdev)
  469. {
  470. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  471. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  472. int id;
  473. struct wm831x_ldo *ldo;
  474. struct resource *res;
  475. int ret, irq;
  476. if (pdata && pdata->wm831x_num)
  477. id = (pdata->wm831x_num * 10) + 1;
  478. else
  479. id = 0;
  480. id = pdev->id - id;
  481. dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
  482. if (pdata == NULL || pdata->ldo[id] == NULL)
  483. return -ENODEV;
  484. ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
  485. if (ldo == NULL) {
  486. dev_err(&pdev->dev, "Unable to allocate private data\n");
  487. return -ENOMEM;
  488. }
  489. ldo->wm831x = wm831x;
  490. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  491. if (res == NULL) {
  492. dev_err(&pdev->dev, "No I/O resource\n");
  493. ret = -EINVAL;
  494. goto err;
  495. }
  496. ldo->base = res->start;
  497. snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
  498. ldo->desc.name = ldo->name;
  499. ldo->desc.id = id;
  500. ldo->desc.type = REGULATOR_VOLTAGE;
  501. ldo->desc.n_voltages = WM831X_ALDO_MAX_SELECTOR + 1;
  502. ldo->desc.ops = &wm831x_aldo_ops;
  503. ldo->desc.owner = THIS_MODULE;
  504. ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
  505. pdata->ldo[id], ldo, NULL);
  506. if (IS_ERR(ldo->regulator)) {
  507. ret = PTR_ERR(ldo->regulator);
  508. dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
  509. id + 1, ret);
  510. goto err;
  511. }
  512. irq = platform_get_irq_byname(pdev, "UV");
  513. ret = request_threaded_irq(irq, NULL, wm831x_ldo_uv_irq,
  514. IRQF_TRIGGER_RISING, ldo->name, ldo);
  515. if (ret != 0) {
  516. dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
  517. irq, ret);
  518. goto err_regulator;
  519. }
  520. platform_set_drvdata(pdev, ldo);
  521. return 0;
  522. err_regulator:
  523. regulator_unregister(ldo->regulator);
  524. err:
  525. return ret;
  526. }
  527. static __devexit int wm831x_aldo_remove(struct platform_device *pdev)
  528. {
  529. struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
  530. free_irq(platform_get_irq_byname(pdev, "UV"), ldo);
  531. regulator_unregister(ldo->regulator);
  532. return 0;
  533. }
  534. static struct platform_driver wm831x_aldo_driver = {
  535. .probe = wm831x_aldo_probe,
  536. .remove = __devexit_p(wm831x_aldo_remove),
  537. .driver = {
  538. .name = "wm831x-aldo",
  539. .owner = THIS_MODULE,
  540. },
  541. };
  542. /*
  543. * Alive LDO
  544. */
  545. #define WM831X_ALIVE_LDO_MAX_SELECTOR 0xf
  546. static int wm831x_alive_ldo_list_voltage(struct regulator_dev *rdev,
  547. unsigned int selector)
  548. {
  549. /* 0.8-1.55V in 50mV steps */
  550. if (selector <= WM831X_ALIVE_LDO_MAX_SELECTOR)
  551. return 800000 + (selector * 50000);
  552. return -EINVAL;
  553. }
  554. static int wm831x_alive_ldo_set_voltage_int(struct regulator_dev *rdev,
  555. int reg,
  556. int min_uV, int max_uV,
  557. unsigned *selector)
  558. {
  559. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  560. struct wm831x *wm831x = ldo->wm831x;
  561. int vsel, ret;
  562. vsel = (min_uV - 800000) / 50000;
  563. ret = wm831x_alive_ldo_list_voltage(rdev, vsel);
  564. if (ret < 0)
  565. return ret;
  566. if (ret < min_uV || ret > max_uV)
  567. return -EINVAL;
  568. *selector = vsel;
  569. return wm831x_set_bits(wm831x, reg, WM831X_LDO11_ON_VSEL_MASK, vsel);
  570. }
  571. static int wm831x_alive_ldo_set_voltage(struct regulator_dev *rdev,
  572. int min_uV, int max_uV,
  573. unsigned *selector)
  574. {
  575. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  576. int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
  577. return wm831x_alive_ldo_set_voltage_int(rdev, reg, min_uV, max_uV,
  578. selector);
  579. }
  580. static int wm831x_alive_ldo_set_suspend_voltage(struct regulator_dev *rdev,
  581. int uV)
  582. {
  583. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  584. int reg = ldo->base + WM831X_ALIVE_LDO_SLEEP_CONTROL;
  585. unsigned selector;
  586. return wm831x_alive_ldo_set_voltage_int(rdev, reg, uV, uV, &selector);
  587. }
  588. static int wm831x_alive_ldo_get_voltage_sel(struct regulator_dev *rdev)
  589. {
  590. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  591. struct wm831x *wm831x = ldo->wm831x;
  592. int reg = ldo->base + WM831X_ALIVE_LDO_ON_CONTROL;
  593. int ret;
  594. ret = wm831x_reg_read(wm831x, reg);
  595. if (ret < 0)
  596. return ret;
  597. ret &= WM831X_LDO11_ON_VSEL_MASK;
  598. return ret;
  599. }
  600. static int wm831x_alive_ldo_get_status(struct regulator_dev *rdev)
  601. {
  602. struct wm831x_ldo *ldo = rdev_get_drvdata(rdev);
  603. struct wm831x *wm831x = ldo->wm831x;
  604. int mask = 1 << rdev_get_id(rdev);
  605. int ret;
  606. /* Is the regulator on? */
  607. ret = wm831x_reg_read(wm831x, WM831X_LDO_STATUS);
  608. if (ret < 0)
  609. return ret;
  610. if (ret & mask)
  611. return REGULATOR_STATUS_ON;
  612. else
  613. return REGULATOR_STATUS_OFF;
  614. }
  615. static struct regulator_ops wm831x_alive_ldo_ops = {
  616. .list_voltage = wm831x_alive_ldo_list_voltage,
  617. .get_voltage_sel = wm831x_alive_ldo_get_voltage_sel,
  618. .set_voltage = wm831x_alive_ldo_set_voltage,
  619. .set_suspend_voltage = wm831x_alive_ldo_set_suspend_voltage,
  620. .get_status = wm831x_alive_ldo_get_status,
  621. .is_enabled = wm831x_ldo_is_enabled,
  622. .enable = wm831x_ldo_enable,
  623. .disable = wm831x_ldo_disable,
  624. };
  625. static __devinit int wm831x_alive_ldo_probe(struct platform_device *pdev)
  626. {
  627. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  628. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  629. int id;
  630. struct wm831x_ldo *ldo;
  631. struct resource *res;
  632. int ret;
  633. if (pdata && pdata->wm831x_num)
  634. id = (pdata->wm831x_num * 10) + 1;
  635. else
  636. id = 0;
  637. id = pdev->id - id;
  638. dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1);
  639. if (pdata == NULL || pdata->ldo[id] == NULL)
  640. return -ENODEV;
  641. ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_ldo), GFP_KERNEL);
  642. if (ldo == NULL) {
  643. dev_err(&pdev->dev, "Unable to allocate private data\n");
  644. return -ENOMEM;
  645. }
  646. ldo->wm831x = wm831x;
  647. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  648. if (res == NULL) {
  649. dev_err(&pdev->dev, "No I/O resource\n");
  650. ret = -EINVAL;
  651. goto err;
  652. }
  653. ldo->base = res->start;
  654. snprintf(ldo->name, sizeof(ldo->name), "LDO%d", id + 1);
  655. ldo->desc.name = ldo->name;
  656. ldo->desc.id = id;
  657. ldo->desc.type = REGULATOR_VOLTAGE;
  658. ldo->desc.n_voltages = WM831X_ALIVE_LDO_MAX_SELECTOR + 1;
  659. ldo->desc.ops = &wm831x_alive_ldo_ops;
  660. ldo->desc.owner = THIS_MODULE;
  661. ldo->regulator = regulator_register(&ldo->desc, &pdev->dev,
  662. pdata->ldo[id], ldo, NULL);
  663. if (IS_ERR(ldo->regulator)) {
  664. ret = PTR_ERR(ldo->regulator);
  665. dev_err(wm831x->dev, "Failed to register LDO%d: %d\n",
  666. id + 1, ret);
  667. goto err;
  668. }
  669. platform_set_drvdata(pdev, ldo);
  670. return 0;
  671. err:
  672. return ret;
  673. }
  674. static __devexit int wm831x_alive_ldo_remove(struct platform_device *pdev)
  675. {
  676. struct wm831x_ldo *ldo = platform_get_drvdata(pdev);
  677. regulator_unregister(ldo->regulator);
  678. return 0;
  679. }
  680. static struct platform_driver wm831x_alive_ldo_driver = {
  681. .probe = wm831x_alive_ldo_probe,
  682. .remove = __devexit_p(wm831x_alive_ldo_remove),
  683. .driver = {
  684. .name = "wm831x-alive-ldo",
  685. .owner = THIS_MODULE,
  686. },
  687. };
  688. static int __init wm831x_ldo_init(void)
  689. {
  690. int ret;
  691. ret = platform_driver_register(&wm831x_gp_ldo_driver);
  692. if (ret != 0)
  693. pr_err("Failed to register WM831x GP LDO driver: %d\n", ret);
  694. ret = platform_driver_register(&wm831x_aldo_driver);
  695. if (ret != 0)
  696. pr_err("Failed to register WM831x ALDO driver: %d\n", ret);
  697. ret = platform_driver_register(&wm831x_alive_ldo_driver);
  698. if (ret != 0)
  699. pr_err("Failed to register WM831x alive LDO driver: %d\n",
  700. ret);
  701. return 0;
  702. }
  703. subsys_initcall(wm831x_ldo_init);
  704. static void __exit wm831x_ldo_exit(void)
  705. {
  706. platform_driver_unregister(&wm831x_alive_ldo_driver);
  707. platform_driver_unregister(&wm831x_aldo_driver);
  708. platform_driver_unregister(&wm831x_gp_ldo_driver);
  709. }
  710. module_exit(wm831x_ldo_exit);
  711. /* Module information */
  712. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  713. MODULE_DESCRIPTION("WM831x LDO driver");
  714. MODULE_LICENSE("GPL");
  715. MODULE_ALIAS("platform:wm831x-ldo");
  716. MODULE_ALIAS("platform:wm831x-aldo");
  717. MODULE_ALIAS("platform:wm831x-aliveldo");