lp8755.c 13 KB

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