lp8755.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * LP8755 High Performance Power Management Unit : System Interface Driver
  3. * (based on rev. 0.26)
  4. * Copyright 2012 Texas Instruments
  5. *
  6. * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/i2c.h>
  16. #include <linux/err.h>
  17. #include <linux/irq.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/gpio.h>
  20. #include <linux/regmap.h>
  21. #include <linux/delay.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/regulator/driver.h>
  24. #include <linux/regulator/machine.h>
  25. #include <linux/platform_data/lp8755.h>
  26. #define LP8755_REG_BUCK0 0x00
  27. #define LP8755_REG_BUCK1 0x03
  28. #define LP8755_REG_BUCK2 0x04
  29. #define LP8755_REG_BUCK3 0x01
  30. #define LP8755_REG_BUCK4 0x05
  31. #define LP8755_REG_BUCK5 0x02
  32. #define LP8755_REG_MAX 0xFF
  33. #define LP8755_BUCK_EN_M BIT(7)
  34. #define LP8755_BUCK_LINEAR_OUT_MAX 0x76
  35. #define LP8755_BUCK_VOUT_M 0x7F
  36. enum bucks {
  37. BUCK0 = 0,
  38. BUCK1,
  39. BUCK2,
  40. BUCK3,
  41. BUCK4,
  42. BUCK5,
  43. };
  44. struct lp8755_mphase {
  45. int nreg;
  46. int buck_num[LP8755_BUCK_MAX];
  47. };
  48. struct lp8755_chip {
  49. struct device *dev;
  50. struct regmap *regmap;
  51. struct lp8755_platform_data *pdata;
  52. int irq;
  53. unsigned int irqmask;
  54. int mphase;
  55. struct regulator_dev *rdev[LP8755_BUCK_MAX];
  56. };
  57. /**
  58. *lp8755_read : read a single register value from lp8755.
  59. *@pchip : device to read from
  60. *@reg : register to read from
  61. *@val : pointer to store read value
  62. */
  63. static int lp8755_read(struct lp8755_chip *pchip, unsigned int reg,
  64. unsigned int *val)
  65. {
  66. return regmap_read(pchip->regmap, reg, val);
  67. }
  68. /**
  69. *lp8755_write : write a single register value to lp8755.
  70. *@pchip : device to write to
  71. *@reg : register to write to
  72. *@val : value to be written
  73. */
  74. static int lp8755_write(struct lp8755_chip *pchip, unsigned int reg,
  75. unsigned int val)
  76. {
  77. return regmap_write(pchip->regmap, reg, val);
  78. }
  79. /**
  80. *lp8755_update_bits : set the values of bit fields in lp8755 register.
  81. *@pchip : device to read from
  82. *@reg : register to update
  83. *@mask : bitmask to be changed
  84. *@val : value for bitmask
  85. */
  86. static int lp8755_update_bits(struct lp8755_chip *pchip, unsigned int reg,
  87. unsigned int mask, unsigned int val)
  88. {
  89. return regmap_update_bits(pchip->regmap, reg, mask, val);
  90. }
  91. static int lp8755_buck_enable_time(struct regulator_dev *rdev)
  92. {
  93. int ret;
  94. unsigned int regval;
  95. enum lp8755_bucks id = rdev_get_id(rdev);
  96. struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
  97. ret = lp8755_read(pchip, 0x12 + id, &regval);
  98. if (ret < 0) {
  99. dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
  100. return ret;
  101. }
  102. return (regval & 0xff) * 100;
  103. }
  104. static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
  105. {
  106. int ret;
  107. unsigned int regbval = 0x0;
  108. enum lp8755_bucks id = rdev_get_id(rdev);
  109. struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
  110. switch (mode) {
  111. case REGULATOR_MODE_FAST:
  112. /* forced pwm mode */
  113. regbval = (0x01 << id);
  114. break;
  115. case REGULATOR_MODE_NORMAL:
  116. /* enable automatic pwm/pfm mode */
  117. ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x00);
  118. if (ret < 0)
  119. goto err_i2c;
  120. break;
  121. case REGULATOR_MODE_IDLE:
  122. /* enable automatic pwm/pfm/lppfm mode */
  123. ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x20);
  124. if (ret < 0)
  125. goto err_i2c;
  126. ret = lp8755_update_bits(pchip, 0x10, 0x01, 0x01);
  127. if (ret < 0)
  128. goto err_i2c;
  129. break;
  130. default:
  131. dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
  132. /* forced pwm mode */
  133. regbval = (0x01 << id);
  134. }
  135. ret = lp8755_update_bits(pchip, 0x06, 0x01 << id, regbval);
  136. if (ret < 0)
  137. goto err_i2c;
  138. return ret;
  139. err_i2c:
  140. dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
  141. return ret;
  142. }
  143. static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
  144. {
  145. int ret;
  146. unsigned int regval;
  147. enum lp8755_bucks id = rdev_get_id(rdev);
  148. struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
  149. ret = lp8755_read(pchip, 0x06, &regval);
  150. if (ret < 0)
  151. goto err_i2c;
  152. /* mode fast means forced pwm mode */
  153. if (regval & (0x01 << id))
  154. return REGULATOR_MODE_FAST;
  155. ret = lp8755_read(pchip, 0x08 + id, &regval);
  156. if (ret < 0)
  157. goto err_i2c;
  158. /* mode idle means automatic pwm/pfm/lppfm mode */
  159. if (regval & 0x20)
  160. return REGULATOR_MODE_IDLE;
  161. /* mode normal means automatic pwm/pfm mode */
  162. return REGULATOR_MODE_NORMAL;
  163. err_i2c:
  164. dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
  165. return 0;
  166. }
  167. static int lp8755_buck_set_ramp(struct regulator_dev *rdev, int ramp)
  168. {
  169. int ret;
  170. unsigned int regval = 0x00;
  171. enum lp8755_bucks id = rdev_get_id(rdev);
  172. struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
  173. /* uV/us */
  174. switch (ramp) {
  175. case 0 ... 230:
  176. regval = 0x07;
  177. break;
  178. case 231 ... 470:
  179. regval = 0x06;
  180. break;
  181. case 471 ... 940:
  182. regval = 0x05;
  183. break;
  184. case 941 ... 1900:
  185. regval = 0x04;
  186. break;
  187. case 1901 ... 3800:
  188. regval = 0x03;
  189. break;
  190. case 3801 ... 7500:
  191. regval = 0x02;
  192. break;
  193. case 7501 ... 15000:
  194. regval = 0x01;
  195. break;
  196. case 15001 ... 30000:
  197. regval = 0x00;
  198. break;
  199. default:
  200. dev_err(pchip->dev,
  201. "Not supported ramp value %d %s\n", ramp, __func__);
  202. return -EINVAL;
  203. }
  204. ret = lp8755_update_bits(pchip, 0x07 + id, 0x07, regval);
  205. if (ret < 0)
  206. goto err_i2c;
  207. return ret;
  208. err_i2c:
  209. dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
  210. return ret;
  211. }
  212. static struct regulator_ops lp8755_buck_ops = {
  213. .list_voltage = regulator_list_voltage_linear,
  214. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  215. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  216. .enable = regulator_enable_regmap,
  217. .disable = regulator_disable_regmap,
  218. .is_enabled = regulator_is_enabled_regmap,
  219. .enable_time = lp8755_buck_enable_time,
  220. .set_mode = lp8755_buck_set_mode,
  221. .get_mode = lp8755_buck_get_mode,
  222. .set_ramp_delay = lp8755_buck_set_ramp,
  223. };
  224. #define lp8755_rail(_id) "lp8755_buck"#_id
  225. #define lp8755_buck_init(_id)\
  226. {\
  227. .constraints = {\
  228. .name = lp8755_rail(_id),\
  229. .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
  230. .min_uV = 500000,\
  231. .max_uV = 1675000,\
  232. },\
  233. }
  234. static struct regulator_init_data lp8755_reg_default[LP8755_BUCK_MAX] = {
  235. [BUCK0] = lp8755_buck_init(0),
  236. [BUCK1] = lp8755_buck_init(1),
  237. [BUCK2] = lp8755_buck_init(2),
  238. [BUCK3] = lp8755_buck_init(3),
  239. [BUCK4] = lp8755_buck_init(4),
  240. [BUCK5] = lp8755_buck_init(5),
  241. };
  242. static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = {
  243. {3, {BUCK0, BUCK3, BUCK5}
  244. },
  245. {6, {BUCK0, BUCK1, BUCK2, BUCK3, BUCK4, BUCK5}
  246. },
  247. {5, {BUCK0, BUCK2, BUCK3, BUCK4, BUCK5}
  248. },
  249. {4, {BUCK0, BUCK3, BUCK4, BUCK5}
  250. },
  251. {3, {BUCK0, BUCK4, BUCK5}
  252. },
  253. {2, {BUCK0, BUCK5}
  254. },
  255. {1, {BUCK0}
  256. },
  257. {2, {BUCK0, BUCK3}
  258. },
  259. {4, {BUCK0, BUCK2, BUCK3, BUCK5}
  260. },
  261. };
  262. static int lp8755_init_data(struct lp8755_chip *pchip)
  263. {
  264. unsigned int regval;
  265. int ret, icnt, buck_num;
  266. struct lp8755_platform_data *pdata = pchip->pdata;
  267. /* read back muti-phase configuration */
  268. ret = lp8755_read(pchip, 0x3D, &regval);
  269. if (ret < 0)
  270. goto out_i2c_error;
  271. pchip->mphase = regval & 0x07;
  272. /* set default data based on multi-phase config */
  273. for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
  274. buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
  275. pdata->buck_data[buck_num] = &lp8755_reg_default[buck_num];
  276. }
  277. return ret;
  278. out_i2c_error:
  279. dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
  280. return ret;
  281. }
  282. #define lp8755_buck_desc(_id)\
  283. {\
  284. .name = lp8755_rail(_id),\
  285. .id = LP8755_BUCK##_id,\
  286. .ops = &lp8755_buck_ops,\
  287. .n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
  288. .uV_step = 10000,\
  289. .min_uV = 500000,\
  290. .type = REGULATOR_VOLTAGE,\
  291. .owner = THIS_MODULE,\
  292. .enable_reg = LP8755_REG_BUCK##_id,\
  293. .enable_mask = LP8755_BUCK_EN_M,\
  294. .vsel_reg = LP8755_REG_BUCK##_id,\
  295. .vsel_mask = LP8755_BUCK_VOUT_M,\
  296. }
  297. static struct regulator_desc lp8755_regulators[] = {
  298. lp8755_buck_desc(0),
  299. lp8755_buck_desc(1),
  300. lp8755_buck_desc(2),
  301. lp8755_buck_desc(3),
  302. lp8755_buck_desc(4),
  303. lp8755_buck_desc(5),
  304. };
  305. static int lp8755_regulator_init(struct lp8755_chip *pchip)
  306. {
  307. int ret, icnt, buck_num;
  308. struct lp8755_platform_data *pdata = pchip->pdata;
  309. struct regulator_config rconfig = { };
  310. rconfig.regmap = pchip->regmap;
  311. rconfig.dev = pchip->dev;
  312. rconfig.driver_data = pchip;
  313. for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
  314. buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
  315. rconfig.init_data = pdata->buck_data[buck_num];
  316. rconfig.of_node = pchip->dev->of_node;
  317. pchip->rdev[buck_num] =
  318. regulator_register(&lp8755_regulators[buck_num], &rconfig);
  319. if (IS_ERR(pchip->rdev[buck_num])) {
  320. ret = PTR_ERR(pchip->rdev[buck_num]);
  321. dev_err(pchip->dev, "regulator init failed: buck 0\n");
  322. goto err_buck;
  323. }
  324. }
  325. return 0;
  326. err_buck:
  327. for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
  328. if (pchip->rdev[icnt] != NULL)
  329. regulator_unregister(pchip->rdev[icnt]);
  330. return ret;
  331. }
  332. static irqreturn_t lp8755_irq_handler(int irq, void *data)
  333. {
  334. int ret, icnt;
  335. unsigned int flag0, flag1;
  336. struct lp8755_chip *pchip = data;
  337. /* read flag0 register */
  338. ret = lp8755_read(pchip, 0x0D, &flag0);
  339. if (ret < 0)
  340. goto err_i2c;
  341. /* clear flag register to pull up int. pin */
  342. ret = lp8755_write(pchip, 0x0D, 0x00);
  343. if (ret < 0)
  344. goto err_i2c;
  345. /* sent power fault detection event to specific regulator */
  346. for (icnt = 0; icnt < 6; icnt++)
  347. if ((flag0 & (0x4 << icnt))
  348. && (pchip->irqmask & (0x04 << icnt))
  349. && (pchip->rdev[icnt] != NULL))
  350. regulator_notifier_call_chain(pchip->rdev[icnt],
  351. LP8755_EVENT_PWR_FAULT,
  352. NULL);
  353. /* read flag1 register */
  354. ret = lp8755_read(pchip, 0x0E, &flag1);
  355. if (ret < 0)
  356. goto err_i2c;
  357. /* clear flag register to pull up int. pin */
  358. ret = lp8755_write(pchip, 0x0E, 0x00);
  359. if (ret < 0)
  360. goto err_i2c;
  361. /* send OCP event to all regualtor devices */
  362. if ((flag1 & 0x01) && (pchip->irqmask & 0x01))
  363. for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
  364. if (pchip->rdev[icnt] != NULL)
  365. regulator_notifier_call_chain(pchip->rdev[icnt],
  366. LP8755_EVENT_OCP,
  367. NULL);
  368. /* send OVP event to all regualtor devices */
  369. if ((flag1 & 0x02) && (pchip->irqmask & 0x02))
  370. for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
  371. if (pchip->rdev[icnt] != NULL)
  372. regulator_notifier_call_chain(pchip->rdev[icnt],
  373. LP8755_EVENT_OVP,
  374. NULL);
  375. return IRQ_HANDLED;
  376. err_i2c:
  377. dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
  378. return IRQ_NONE;
  379. }
  380. static int lp8755_int_config(struct lp8755_chip *pchip)
  381. {
  382. int ret;
  383. unsigned int regval;
  384. if (pchip->irq == 0) {
  385. dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
  386. return 0;
  387. }
  388. ret = lp8755_read(pchip, 0x0F, &regval);
  389. if (ret < 0)
  390. goto err_i2c;
  391. pchip->irqmask = regval;
  392. ret = request_threaded_irq(pchip->irq, NULL, lp8755_irq_handler,
  393. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  394. "lp8755-irq", pchip);
  395. if (ret)
  396. return ret;
  397. return ret;
  398. err_i2c:
  399. dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
  400. return ret;
  401. }
  402. static const struct regmap_config lp8755_regmap = {
  403. .reg_bits = 8,
  404. .val_bits = 8,
  405. .max_register = LP8755_REG_MAX,
  406. };
  407. static int lp8755_probe(struct i2c_client *client,
  408. const struct i2c_device_id *id)
  409. {
  410. int ret, icnt;
  411. struct lp8755_chip *pchip;
  412. struct lp8755_platform_data *pdata = client->dev.platform_data;
  413. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  414. dev_err(&client->dev, "i2c functionality check fail.\n");
  415. return -EOPNOTSUPP;
  416. }
  417. pchip = devm_kzalloc(&client->dev,
  418. sizeof(struct lp8755_chip), GFP_KERNEL);
  419. if (!pchip)
  420. return -ENOMEM;
  421. pchip->dev = &client->dev;
  422. pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap);
  423. if (IS_ERR(pchip->regmap)) {
  424. ret = PTR_ERR(pchip->regmap);
  425. dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
  426. return ret;
  427. }
  428. i2c_set_clientdata(client, pchip);
  429. if (pdata != NULL) {
  430. pchip->pdata = pdata;
  431. pchip->mphase = pdata->mphase;
  432. } else {
  433. pchip->pdata = devm_kzalloc(pchip->dev,
  434. sizeof(struct lp8755_platform_data),
  435. GFP_KERNEL);
  436. if (!pchip->pdata)
  437. return -ENOMEM;
  438. ret = lp8755_init_data(pchip);
  439. if (ret < 0)
  440. goto err_chip_init;
  441. }
  442. ret = lp8755_regulator_init(pchip);
  443. if (ret < 0)
  444. goto err_regulator;
  445. pchip->irq = client->irq;
  446. ret = lp8755_int_config(pchip);
  447. if (ret < 0)
  448. goto err_irq;
  449. return ret;
  450. err_irq:
  451. dev_err(&client->dev, "fail to irq config\n");
  452. for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
  453. regulator_unregister(pchip->rdev[icnt]);
  454. err_regulator:
  455. dev_err(&client->dev, "fail to initialize regulators\n");
  456. /* output disable */
  457. for (icnt = 0; icnt < 0x06; icnt++)
  458. lp8755_write(pchip, icnt, 0x00);
  459. err_chip_init:
  460. dev_err(&client->dev, "fail to initialize chip\n");
  461. return ret;
  462. }
  463. static int lp8755_remove(struct i2c_client *client)
  464. {
  465. int icnt;
  466. struct lp8755_chip *pchip = i2c_get_clientdata(client);
  467. for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
  468. regulator_unregister(pchip->rdev[icnt]);
  469. for (icnt = 0; icnt < 0x06; icnt++)
  470. lp8755_write(pchip, icnt, 0x00);
  471. if (pchip->irq != 0)
  472. free_irq(pchip->irq, pchip);
  473. return 0;
  474. }
  475. static const struct i2c_device_id lp8755_id[] = {
  476. {LP8755_NAME, 0},
  477. {}
  478. };
  479. MODULE_DEVICE_TABLE(i2c, lp8755_id);
  480. static struct i2c_driver lp8755_i2c_driver = {
  481. .driver = {
  482. .name = LP8755_NAME,
  483. },
  484. .probe = lp8755_probe,
  485. .remove = __devexit_p(lp8755_remove),
  486. .id_table = lp8755_id,
  487. };
  488. static int __init lp8755_init(void)
  489. {
  490. return i2c_add_driver(&lp8755_i2c_driver);
  491. }
  492. subsys_initcall(lp8755_init);
  493. static void __exit lp8755_exit(void)
  494. {
  495. i2c_del_driver(&lp8755_i2c_driver);
  496. }
  497. module_exit(lp8755_exit);
  498. MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
  499. MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
  500. MODULE_LICENSE("GPL v2");