max77826.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * max77826.c - Regulator driver for the Maxim 77826
  3. *
  4. * Copyright (C) 2013 Samsung Electronics
  5. * Chiwoong Byun <woong.byun@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * This driver is based on max77686.c
  22. */
  23. #include <linux/bug.h>
  24. #include <linux/err.h>
  25. #include <linux/i2c.h>
  26. #include <linux/gpio.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/delay.h>
  30. #include <linux/regulator/driver.h>
  31. #include <linux/regulator/max77826.h>
  32. #include <linux/slab.h>
  33. #include <linux/of_gpio.h>
  34. struct voltage_map_desc {
  35. int min;
  36. int max;
  37. int step;
  38. unsigned int n_bits;
  39. };
  40. static struct voltage_map_desc ldo_nmos_voltage_map_desc = {
  41. .min = 600000, .max = 2187500, .step = 12500, .n_bits = 7,
  42. };
  43. static struct voltage_map_desc ldo_pmos_voltage_map_desc = {
  44. .min = 800000, .max = 3975000, .step = 25000, .n_bits = 7,
  45. };
  46. static struct voltage_map_desc buck_voltage_map_desc = {
  47. .min = 500000, .max = 1800000, .step = 6250, .n_bits = 8,
  48. };
  49. static struct voltage_map_desc buck_boost_voltage_map_desc = {
  50. .min = 2600000, .max = 4187500, .step = 12500, .n_bits = 7,
  51. };
  52. static struct voltage_map_desc *reg_voltage_map[] = {
  53. [MAX77826_LDO1] = &ldo_nmos_voltage_map_desc,
  54. [MAX77826_LDO2] = &ldo_nmos_voltage_map_desc,
  55. [MAX77826_LDO3] = &ldo_nmos_voltage_map_desc,
  56. [MAX77826_LDO4] = &ldo_pmos_voltage_map_desc,
  57. [MAX77826_LDO5] = &ldo_pmos_voltage_map_desc,
  58. [MAX77826_LDO6] = &ldo_pmos_voltage_map_desc,
  59. [MAX77826_LDO7] = &ldo_pmos_voltage_map_desc,
  60. [MAX77826_LDO8] = &ldo_pmos_voltage_map_desc,
  61. [MAX77826_LDO9] = &ldo_pmos_voltage_map_desc,
  62. [MAX77826_LDO10] = &ldo_pmos_voltage_map_desc,
  63. [MAX77826_LDO11] = &ldo_pmos_voltage_map_desc,
  64. [MAX77826_LDO12] = &ldo_pmos_voltage_map_desc,
  65. [MAX77826_LDO13] = &ldo_pmos_voltage_map_desc,
  66. [MAX77826_LDO14] = &ldo_pmos_voltage_map_desc,
  67. [MAX77826_LDO15] = &ldo_pmos_voltage_map_desc,
  68. [MAX77826_BUCK1] = &buck_voltage_map_desc,
  69. [MAX77826_BUCK2] = &buck_boost_voltage_map_desc,
  70. };
  71. int max77826_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  72. {
  73. struct max77826_dev *max77826 = i2c_get_clientdata(i2c);
  74. int ret;
  75. mutex_lock(&max77826->io_lock);
  76. ret = i2c_smbus_read_byte_data(i2c, reg);
  77. mutex_unlock(&max77826->io_lock);
  78. if (ret < 0)
  79. return ret;
  80. ret &= 0xff;
  81. *dest = ret;
  82. return 0;
  83. }
  84. int max77826_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  85. {
  86. struct max77826_dev *max77826 = i2c_get_clientdata(i2c);
  87. int ret;
  88. mutex_lock(&max77826->io_lock);
  89. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  90. mutex_unlock(&max77826->io_lock);
  91. if (ret < 0)
  92. return ret;
  93. return 0;
  94. }
  95. int max77826_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  96. {
  97. struct max77826_dev *max77826 = i2c_get_clientdata(i2c);
  98. int ret;
  99. mutex_lock(&max77826->io_lock);
  100. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  101. mutex_unlock(&max77826->io_lock);
  102. return ret;
  103. }
  104. int max77826_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  105. {
  106. struct max77826_dev *max77826 = i2c_get_clientdata(i2c);
  107. int ret;
  108. mutex_lock(&max77826->io_lock);
  109. ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
  110. mutex_unlock(&max77826->io_lock);
  111. if (ret < 0)
  112. return ret;
  113. return 0;
  114. }
  115. int max77826_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  116. {
  117. struct max77826_dev *max77826 = i2c_get_clientdata(i2c);
  118. int ret;
  119. mutex_lock(&max77826->io_lock);
  120. ret = i2c_smbus_read_byte_data(i2c, reg);
  121. if (ret >= 0) {
  122. u8 old_val = ret & 0xff;
  123. u8 new_val = (val & mask) | (old_val & (~mask));
  124. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  125. }
  126. mutex_unlock(&max77826->io_lock);
  127. return ret;
  128. }
  129. static int max77826_get_op_register(struct regulator_dev *rdev,
  130. int *reg, int *mask, int *pattern)
  131. {
  132. unsigned int rid = rdev_get_id(rdev);
  133. switch (rid) {
  134. case MAX77826_LDO1 ... MAX77826_LDO15:
  135. *reg = MAX77826_REG_LDO_OPMD1 + (rid - MAX77826_LDO1) / 4;
  136. *mask = 1 << (((rid - MAX77826_LDO1) % 4) * 2 + 1);
  137. *pattern = 1 << (((rid - MAX77826_LDO1) % 4) * 2 + 1);
  138. break;
  139. case MAX77826_BUCK1 ... MAX77826_BUCK2:
  140. *reg = MAX77826_REG_B_BB_OPMD;
  141. *mask = 1 << (((rid - MAX77826_BUCK1) % 2) * 2 + 1);
  142. *pattern = 1 << (((rid - MAX77826_BUCK1) % 2) * 2 + 1);
  143. break;
  144. default:
  145. return -EINVAL;
  146. }
  147. pr_info("%s: id=%d, reg=0x%x, mask=0x%x, pattern=0x%x\n",
  148. __func__, rid, *reg, *mask, *pattern);
  149. return 0;
  150. }
  151. static int max77826_get_voltage_register(struct regulator_dev *rdev,
  152. int *reg, int *shift, int *mask)
  153. {
  154. int rid = rdev_get_id(rdev);
  155. switch (rid) {
  156. case MAX77826_LDO1 ... MAX77826_LDO15:
  157. *reg = MAX77826_REG_LDO1_CFG + (rid - MAX77826_LDO1);
  158. *shift = 0;
  159. *mask = 0x7F;
  160. break;
  161. case MAX77826_BUCK1:
  162. *reg = MAX77826_REG_BUCK_VOUT;
  163. *shift = 0;
  164. *mask = 0xFF;
  165. break;
  166. case MAX77826_BUCK2:
  167. *reg = MAX77826_REG_BB_VOUT;
  168. *shift = 0;
  169. *mask = 0x7F;
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. pr_info("%s: id=%d, reg=0x%x, shift=0x%x, mask=0x%x\n",
  175. __func__, rid, *reg, *shift, *mask);
  176. return 0;
  177. }
  178. static int max77826_ldo_is_enabled(struct regulator_dev *rdev)
  179. {
  180. struct max77826_dev *max77826 = rdev_get_drvdata(rdev);
  181. int ret = 0, reg = 0, mask = 0, pattern = 0;
  182. u8 val;
  183. ret = max77826_get_op_register(rdev, &reg, &mask, &pattern);
  184. if (ret) {
  185. pr_err("%s: id=%d, error=%d\n", __func__, rdev_get_id(rdev), ret);
  186. return ret;
  187. }
  188. ret = max77826_read_reg(max77826->i2c, reg, &val);
  189. if (ret) {
  190. return ret;
  191. }
  192. pr_debug("%s: id=%d, ret=%d, val=0x%x, mask=0x%x, pattern=0x%x\n",
  193. __func__, rdev_get_id(rdev), (val & mask) == pattern,
  194. val, mask, pattern);
  195. return (val & mask) == pattern;
  196. }
  197. static int max77826_ldo_enable(struct regulator_dev *rdev)
  198. {
  199. struct max77826_dev *max77826 = rdev_get_drvdata(rdev);
  200. int ret = 0, reg = 0, mask = 0, pattern = 0;
  201. ret = max77826_get_op_register(rdev, &reg, &mask, &pattern);
  202. if (ret) {
  203. pr_err("%s: id=%d, error=%d\n", __func__, rdev_get_id(rdev), ret);
  204. return ret;
  205. }
  206. pr_debug("%s: id=%d, reg=0x%x, mask=0x%x, pattern=0x%x\n",
  207. __func__, rdev_get_id(rdev), reg, mask, pattern);
  208. return max77826_update_reg(max77826->i2c, reg, pattern, mask);
  209. }
  210. static int max77826_ldo_disable(struct regulator_dev *rdev)
  211. {
  212. struct max77826_dev *max77826 = rdev_get_drvdata(rdev);
  213. int ret = 0, reg = 0, mask = 0, pattern = 0;
  214. ret = max77826_get_op_register(rdev, &reg, &mask, &pattern);
  215. if (ret) {
  216. pr_err("%s: id=%d, error=%d\n", __func__, rdev_get_id(rdev), ret);
  217. return ret;
  218. }
  219. pr_debug("%s: id=%d, reg=0x%x, mask=0x%x, pattern=0x%x\n",
  220. __func__, rdev_get_id(rdev), reg, mask, pattern);
  221. return max77826_update_reg(max77826->i2c, reg, ~mask, mask);
  222. }
  223. static int max77826_ldo_list_voltage(struct regulator_dev *rdev,
  224. unsigned int selector)
  225. {
  226. const struct voltage_map_desc *desc;
  227. int rid = rdev_get_id(rdev);
  228. int val;
  229. if (rid >= ARRAY_SIZE(reg_voltage_map) ||
  230. rid < 0)
  231. return -EINVAL;
  232. desc = reg_voltage_map[rid];
  233. if (desc == NULL)
  234. return -EINVAL;
  235. val = desc->min + desc->step * selector;
  236. if (val > desc->max)
  237. return -EINVAL;
  238. pr_debug("%s: id=%d, val=%d\n", __func__, rdev_get_id(rdev), val);
  239. return val;
  240. }
  241. static int max77826_ldo_get_voltage(struct regulator_dev *rdev)
  242. {
  243. struct max77826_dev *max77826 = rdev_get_drvdata(rdev);
  244. int reg, shift, mask, ret;
  245. u8 val;
  246. ret = max77826_get_voltage_register(rdev, &reg, &shift, &mask);
  247. if (ret) {
  248. pr_err("%s: id=%d, error=%d\n", __func__, rdev_get_id(rdev), ret);
  249. return ret;
  250. }
  251. ret = max77826_read_reg(max77826->i2c, reg, &val);
  252. if (ret)
  253. return ret;
  254. val = (val & mask) >> shift;
  255. pr_debug("%s: id=%d, reg=0x%x, mask=0x%x, val=0x%x\n",
  256. __func__, rdev_get_id(rdev), reg, mask, val);
  257. return max77826_ldo_list_voltage(rdev, val);
  258. }
  259. static inline int max77826_get_voltage_proper_val(
  260. const struct voltage_map_desc *desc,
  261. int min_vol, int max_vol)
  262. {
  263. int i = 0;
  264. if (desc == NULL)
  265. return -EINVAL;
  266. if (max_vol < desc->min || min_vol > desc->max)
  267. return -EINVAL;
  268. while (desc->min + desc->step * i < min_vol &&
  269. desc->min + desc->step * i < desc->max)
  270. i++;
  271. if (desc->min + desc->step * i > max_vol)
  272. return -EINVAL;
  273. if (i >= (1 << desc->n_bits))
  274. return -EINVAL;
  275. return i;
  276. }
  277. static int max77826_ldo_set_voltage(struct regulator_dev *rdev,
  278. int min_uV, int max_uV, unsigned *selector)
  279. {
  280. struct max77826_dev *max77826 = rdev_get_drvdata(rdev);
  281. const struct voltage_map_desc *desc;
  282. int rid = rdev_get_id(rdev);
  283. int reg, shift, mask, ret;
  284. int i;
  285. u8 val;
  286. desc = reg_voltage_map[rid];
  287. i = max77826_get_voltage_proper_val(desc, min_uV, max_uV);
  288. if (i < 0)
  289. return i;
  290. ret = max77826_get_voltage_register(rdev, &reg, &shift, &mask);
  291. if (ret) {
  292. pr_err("%s: id=%d, error=%d\n", __func__, rdev_get_id(rdev), ret);
  293. return ret;
  294. }
  295. max77826_read_reg(max77826->i2c, reg, &val);
  296. val = (val & mask) >> shift;
  297. pr_info("%s: id=%d, reg=0x%x, mask=0x%x, val=0x%x, new=0x%x\n",
  298. __func__, rdev_get_id(rdev), reg, mask, val, i);
  299. ret = max77826_update_reg(max77826->i2c, reg, i << shift, mask << shift);
  300. *selector = i;
  301. switch (rid) {
  302. case MAX77826_BUCK1:
  303. if (val < i)
  304. udelay(DIV_ROUND_UP(desc->step * (i - val), 12500));
  305. break;
  306. default:
  307. break;
  308. }
  309. return ret;
  310. }
  311. static struct regulator_ops max77826_ldo_ops = {
  312. .is_enabled = max77826_ldo_is_enabled,
  313. .enable = max77826_ldo_enable,
  314. .disable = max77826_ldo_disable,
  315. .list_voltage = max77826_ldo_list_voltage,
  316. .get_voltage = max77826_ldo_get_voltage,
  317. .set_voltage = max77826_ldo_set_voltage,
  318. };
  319. #define regulator_desc_ldo(num) { \
  320. .name = "max77826_ldo"#num, \
  321. .id = MAX77826_LDO##num, \
  322. .ops = &max77826_ldo_ops, \
  323. .type = REGULATOR_VOLTAGE, \
  324. .owner = THIS_MODULE, \
  325. }
  326. #define regulator_desc_buck(num) { \
  327. .name = "max77826_buck"#num, \
  328. .id = MAX77826_BUCK##num, \
  329. .ops = &max77826_ldo_ops, \
  330. .type = REGULATOR_VOLTAGE, \
  331. .owner = THIS_MODULE, \
  332. }
  333. static struct regulator_desc regulators[] = {
  334. regulator_desc_ldo(1),
  335. regulator_desc_ldo(2),
  336. regulator_desc_ldo(3),
  337. regulator_desc_ldo(4),
  338. regulator_desc_ldo(5),
  339. regulator_desc_ldo(6),
  340. regulator_desc_ldo(7),
  341. regulator_desc_ldo(8),
  342. regulator_desc_ldo(9),
  343. regulator_desc_ldo(10),
  344. regulator_desc_ldo(11),
  345. regulator_desc_ldo(12),
  346. regulator_desc_ldo(13),
  347. regulator_desc_ldo(14),
  348. regulator_desc_ldo(15),
  349. regulator_desc_buck(1),
  350. regulator_desc_buck(2),
  351. };
  352. static int __devinit max77826_setup_regulators(struct max77826_dev *max77826,
  353. struct max77826_platform_data *pdata)
  354. {
  355. int i, err;
  356. max77826->num_regulators = pdata->num_regulators;
  357. max77826->rdev = kcalloc(pdata->num_regulators,
  358. sizeof(struct regulator_dev *), GFP_KERNEL);
  359. if (!max77826->rdev) {
  360. err = -ENOMEM;
  361. goto err_nomem;
  362. }
  363. /* Register the regulators */
  364. for (i = 0; i < pdata->num_regulators; i++) {
  365. struct max77826_regulator_subdev *reg = &pdata->regulators[i];
  366. max77826->rdev[i] = regulator_register(&regulators[reg->id],
  367. max77826->dev, reg->initdata, max77826, NULL);
  368. if (IS_ERR(max77826->rdev[i])) {
  369. err = PTR_ERR(max77826->rdev[i]);
  370. dev_err(max77826->dev, "regulator init failed: %d\n",
  371. err);
  372. goto error;
  373. }
  374. }
  375. return 0;
  376. error:
  377. while (--i >= 0)
  378. regulator_unregister(max77826->rdev[i]);
  379. kfree(max77826->rdev);
  380. max77826->rdev = NULL;
  381. err_nomem:
  382. return err;
  383. }
  384. #ifdef CONFIG_OF
  385. static int max77826_pmic_dt_parse_pdata(struct i2c_client *i2c,
  386. struct max77826_platform_data *pdata)
  387. {
  388. return 0;
  389. }
  390. #else
  391. static int max77826_pmic_dt_parse_pdata(struct i2c_client *i2c,
  392. struct max77826_platform_data *pdata)
  393. {
  394. return 0;
  395. }
  396. #endif /* CONFIG_OF */
  397. static int __devinit max77826_i2c_probe(struct i2c_client *i2c,
  398. const struct i2c_device_id *id)
  399. {
  400. struct max77826_dev *max77826;
  401. struct max77826_platform_data *pdata;
  402. int ret;
  403. pr_info("%s\n", __func__);
  404. /*Check I2C functionality */
  405. ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
  406. if (!ret) {
  407. printk(KERN_ERR "%s: No I2C functionality found\n", __func__);
  408. ret = -ENODEV;
  409. goto err_i2c_fail;
  410. }
  411. if (i2c->dev.of_node) {
  412. pr_info("%s: of_node\n", __func__);
  413. pdata = devm_kzalloc(&i2c->dev,
  414. sizeof(struct max77826_platform_data), GFP_KERNEL);
  415. if (!pdata) {
  416. dev_err(&i2c->dev,
  417. "%s: Failed to allocate memory\n", __func__);
  418. return -ENOMEM;
  419. }
  420. ret = max77826_pmic_dt_parse_pdata(i2c, pdata);
  421. if (ret)
  422. return ret;
  423. } else {
  424. pr_info("%s: pdata\n", __func__);
  425. pdata = i2c->dev.platform_data;
  426. if (!pdata) {
  427. dev_err(&i2c->dev, "%s: no pdata\n", __func__);
  428. ret = -ENODEV;
  429. goto err_i2c_fail;
  430. }
  431. }
  432. max77826 = kzalloc(sizeof(struct max77826_dev), GFP_KERNEL);
  433. if (!max77826)
  434. return -ENOMEM;
  435. max77826->i2c = i2c;
  436. max77826->dev = &i2c->dev;
  437. mutex_init(&max77826->io_lock);
  438. i2c_set_clientdata(i2c, max77826);
  439. {
  440. u8 reg = 0;
  441. max77826_read_reg(max77826->i2c, MAX77826_REG_DEVICE_ID, &reg);
  442. pr_info("%s: DEVICE_ID: 0x%x\n", __func__, reg);
  443. }
  444. ret = max77826_setup_regulators(max77826, pdata);
  445. if (ret < 0)
  446. goto err_detect;
  447. return 0;
  448. err_detect:
  449. kfree(max77826);
  450. err_i2c_fail:
  451. return ret;
  452. }
  453. static int __devexit max77826_i2c_remove(struct i2c_client *i2c)
  454. {
  455. struct max77826_dev *max77826 = i2c_get_clientdata(i2c);
  456. int i;
  457. for (i = 0; i < max77826->num_regulators; i++)
  458. regulator_unregister(max77826->rdev[i]);
  459. kfree(max77826->rdev);
  460. kfree(max77826);
  461. return 0;
  462. }
  463. static const struct i2c_device_id max77826_i2c_id[] = {
  464. { "max77826", 0 },
  465. { }
  466. };
  467. MODULE_DEVICE_TABLE(i2c, max77826_i2c_id);
  468. #ifdef CONFIG_OF
  469. static struct of_device_id max77826_match_table[] = {
  470. { .compatible = "max77826,max77826-regulator",},
  471. { },
  472. };
  473. #endif
  474. static struct i2c_driver max77826_i2c_driver = {
  475. .driver = {
  476. .name = "max77826",
  477. .owner = THIS_MODULE,
  478. #ifdef CONFIG_OF
  479. .of_match_table = max77826_match_table,
  480. #endif
  481. },
  482. .probe = max77826_i2c_probe,
  483. .remove = __devexit_p(max77826_i2c_remove),
  484. .id_table = max77826_i2c_id,
  485. };
  486. static int __init max77826_module_init(void)
  487. {
  488. pr_info("%s\n", __func__);
  489. return i2c_add_driver(&max77826_i2c_driver);
  490. }
  491. subsys_initcall(max77826_module_init);
  492. static void __exit max77826_module_exit(void)
  493. {
  494. i2c_del_driver(&max77826_i2c_driver);
  495. }
  496. module_exit(max77826_module_exit);
  497. MODULE_DESCRIPTION("MAXIM 77826 Regulator Driver");
  498. MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
  499. MODULE_LICENSE("GPL");