lp8755.c 13 KB

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