max8998.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // max8998.c - Voltage regulator driver for the Maxim 8998
  4. //
  5. // Copyright (C) 2009-2010 Samsung Electronics
  6. // Kyungmin Park <kyungmin.park@samsung.com>
  7. // Marek Szyprowski <m.szyprowski@samsung.com>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/i2c.h>
  11. #include <linux/err.h>
  12. #include <linux/gpio.h>
  13. #include <linux/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/of_regulator.h>
  21. #include <linux/mfd/max8998.h>
  22. #include <linux/mfd/max8998-private.h>
  23. struct max8998_data {
  24. struct device *dev;
  25. struct max8998_dev *iodev;
  26. int num_regulators;
  27. u8 buck1_vol[4]; /* voltages for selection */
  28. u8 buck2_vol[2];
  29. unsigned int buck1_idx; /* index to last changed voltage */
  30. /* value in a set */
  31. unsigned int buck2_idx;
  32. };
  33. static int max8998_get_enable_register(struct regulator_dev *rdev,
  34. int *reg, int *shift)
  35. {
  36. int ldo = rdev_get_id(rdev);
  37. switch (ldo) {
  38. case MAX8998_LDO2 ... MAX8998_LDO5:
  39. *reg = MAX8998_REG_ONOFF1;
  40. *shift = 3 - (ldo - MAX8998_LDO2);
  41. break;
  42. case MAX8998_LDO6 ... MAX8998_LDO13:
  43. *reg = MAX8998_REG_ONOFF2;
  44. *shift = 7 - (ldo - MAX8998_LDO6);
  45. break;
  46. case MAX8998_LDO14 ... MAX8998_LDO17:
  47. *reg = MAX8998_REG_ONOFF3;
  48. *shift = 7 - (ldo - MAX8998_LDO14);
  49. break;
  50. case MAX8998_BUCK1 ... MAX8998_BUCK4:
  51. *reg = MAX8998_REG_ONOFF1;
  52. *shift = 7 - (ldo - MAX8998_BUCK1);
  53. break;
  54. case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
  55. *reg = MAX8998_REG_ONOFF4;
  56. *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
  57. break;
  58. case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
  59. *reg = MAX8998_REG_CHGR2;
  60. *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. return 0;
  66. }
  67. static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
  68. {
  69. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  70. struct i2c_client *i2c = max8998->iodev->i2c;
  71. int ret, reg, shift = 8;
  72. u8 val;
  73. ret = max8998_get_enable_register(rdev, &reg, &shift);
  74. if (ret)
  75. return ret;
  76. ret = max8998_read_reg(i2c, reg, &val);
  77. if (ret)
  78. return ret;
  79. return val & (1 << shift);
  80. }
  81. static int max8998_ldo_enable(struct regulator_dev *rdev)
  82. {
  83. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  84. struct i2c_client *i2c = max8998->iodev->i2c;
  85. int reg, shift = 8, ret;
  86. ret = max8998_get_enable_register(rdev, &reg, &shift);
  87. if (ret)
  88. return ret;
  89. return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
  90. }
  91. static int max8998_ldo_disable(struct regulator_dev *rdev)
  92. {
  93. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  94. struct i2c_client *i2c = max8998->iodev->i2c;
  95. int reg, shift = 8, ret;
  96. ret = max8998_get_enable_register(rdev, &reg, &shift);
  97. if (ret)
  98. return ret;
  99. return max8998_update_reg(i2c, reg, 0, 1<<shift);
  100. }
  101. static int max8998_get_voltage_register(struct regulator_dev *rdev,
  102. int *_reg, int *_shift, int *_mask)
  103. {
  104. int ldo = rdev_get_id(rdev);
  105. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  106. int reg, shift = 0, mask = 0xff;
  107. switch (ldo) {
  108. case MAX8998_LDO2 ... MAX8998_LDO3:
  109. reg = MAX8998_REG_LDO2_LDO3;
  110. mask = 0xf;
  111. if (ldo == MAX8998_LDO2)
  112. shift = 4;
  113. else
  114. shift = 0;
  115. break;
  116. case MAX8998_LDO4 ... MAX8998_LDO7:
  117. reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
  118. break;
  119. case MAX8998_LDO8 ... MAX8998_LDO9:
  120. reg = MAX8998_REG_LDO8_LDO9;
  121. mask = 0xf;
  122. if (ldo == MAX8998_LDO8)
  123. shift = 4;
  124. else
  125. shift = 0;
  126. break;
  127. case MAX8998_LDO10 ... MAX8998_LDO11:
  128. reg = MAX8998_REG_LDO10_LDO11;
  129. if (ldo == MAX8998_LDO10) {
  130. shift = 5;
  131. mask = 0x7;
  132. } else {
  133. shift = 0;
  134. mask = 0x1f;
  135. }
  136. break;
  137. case MAX8998_LDO12 ... MAX8998_LDO17:
  138. reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
  139. break;
  140. case MAX8998_BUCK1:
  141. reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
  142. break;
  143. case MAX8998_BUCK2:
  144. reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
  145. break;
  146. case MAX8998_BUCK3:
  147. reg = MAX8998_REG_BUCK3;
  148. break;
  149. case MAX8998_BUCK4:
  150. reg = MAX8998_REG_BUCK4;
  151. break;
  152. default:
  153. return -EINVAL;
  154. }
  155. *_reg = reg;
  156. *_shift = shift;
  157. *_mask = mask;
  158. return 0;
  159. }
  160. static int max8998_get_voltage_sel(struct regulator_dev *rdev)
  161. {
  162. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  163. struct i2c_client *i2c = max8998->iodev->i2c;
  164. int reg, shift = 0, mask, ret;
  165. u8 val;
  166. ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
  167. if (ret)
  168. return ret;
  169. ret = max8998_read_reg(i2c, reg, &val);
  170. if (ret)
  171. return ret;
  172. val >>= shift;
  173. val &= mask;
  174. return val;
  175. }
  176. static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
  177. unsigned selector)
  178. {
  179. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  180. struct i2c_client *i2c = max8998->iodev->i2c;
  181. int reg, shift = 0, mask, ret;
  182. ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
  183. if (ret)
  184. return ret;
  185. ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
  186. return ret;
  187. }
  188. static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
  189. {
  190. gpio_set_value(gpio1, v & 0x1);
  191. gpio_set_value(gpio2, (v >> 1) & 0x1);
  192. }
  193. static inline void buck2_gpio_set(int gpio, int v)
  194. {
  195. gpio_set_value(gpio, v & 0x1);
  196. }
  197. static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
  198. unsigned selector)
  199. {
  200. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  201. struct max8998_platform_data *pdata = max8998->iodev->pdata;
  202. struct i2c_client *i2c = max8998->iodev->i2c;
  203. int buck = rdev_get_id(rdev);
  204. int reg, shift = 0, mask, ret, j;
  205. static u8 buck1_last_val;
  206. ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
  207. if (ret)
  208. return ret;
  209. switch (buck) {
  210. case MAX8998_BUCK1:
  211. dev_dbg(max8998->dev,
  212. "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
  213. "buck1_vol3:%d, buck1_vol4:%d\n",
  214. selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
  215. max8998->buck1_vol[2], max8998->buck1_vol[3]);
  216. if (gpio_is_valid(pdata->buck1_set1) &&
  217. gpio_is_valid(pdata->buck1_set2)) {
  218. /* check if requested voltage */
  219. /* value is already defined */
  220. for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
  221. if (max8998->buck1_vol[j] == selector) {
  222. max8998->buck1_idx = j;
  223. buck1_gpio_set(pdata->buck1_set1,
  224. pdata->buck1_set2, j);
  225. goto buck1_exit;
  226. }
  227. }
  228. if (pdata->buck_voltage_lock)
  229. return -EINVAL;
  230. /* no predefine regulator found */
  231. max8998->buck1_idx = (buck1_last_val % 2) + 2;
  232. dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
  233. max8998->buck1_idx);
  234. max8998->buck1_vol[max8998->buck1_idx] = selector;
  235. ret = max8998_get_voltage_register(rdev, &reg,
  236. &shift,
  237. &mask);
  238. ret = max8998_write_reg(i2c, reg, selector);
  239. buck1_gpio_set(pdata->buck1_set1,
  240. pdata->buck1_set2, max8998->buck1_idx);
  241. buck1_last_val++;
  242. buck1_exit:
  243. dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
  244. i2c->name, gpio_get_value(pdata->buck1_set1),
  245. gpio_get_value(pdata->buck1_set2));
  246. break;
  247. } else {
  248. ret = max8998_write_reg(i2c, reg, selector);
  249. }
  250. break;
  251. case MAX8998_BUCK2:
  252. dev_dbg(max8998->dev,
  253. "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
  254. selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
  255. if (gpio_is_valid(pdata->buck2_set3)) {
  256. /* check if requested voltage */
  257. /* value is already defined */
  258. for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
  259. if (max8998->buck2_vol[j] == selector) {
  260. max8998->buck2_idx = j;
  261. buck2_gpio_set(pdata->buck2_set3, j);
  262. goto buck2_exit;
  263. }
  264. }
  265. if (pdata->buck_voltage_lock)
  266. return -EINVAL;
  267. max8998_get_voltage_register(rdev,
  268. &reg, &shift, &mask);
  269. ret = max8998_write_reg(i2c, reg, selector);
  270. max8998->buck2_vol[max8998->buck2_idx] = selector;
  271. buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
  272. buck2_exit:
  273. dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
  274. gpio_get_value(pdata->buck2_set3));
  275. } else {
  276. ret = max8998_write_reg(i2c, reg, selector);
  277. }
  278. break;
  279. case MAX8998_BUCK3:
  280. case MAX8998_BUCK4:
  281. ret = max8998_update_reg(i2c, reg, selector<<shift,
  282. mask<<shift);
  283. break;
  284. }
  285. return ret;
  286. }
  287. static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
  288. unsigned int old_selector,
  289. unsigned int new_selector)
  290. {
  291. struct max8998_data *max8998 = rdev_get_drvdata(rdev);
  292. struct i2c_client *i2c = max8998->iodev->i2c;
  293. int buck = rdev_get_id(rdev);
  294. u8 val = 0;
  295. int difference, ret;
  296. if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
  297. return -EINVAL;
  298. /* Voltage stabilization */
  299. ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
  300. if (ret)
  301. return ret;
  302. /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
  303. /* MAX8998 has ENRAMP bit implemented, so test it*/
  304. if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
  305. return 0;
  306. difference = (new_selector - old_selector) * rdev->desc->uV_step / 1000;
  307. if (difference > 0)
  308. return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
  309. return 0;
  310. }
  311. static const struct regulator_ops max8998_ldo_ops = {
  312. .list_voltage = regulator_list_voltage_linear,
  313. .map_voltage = regulator_map_voltage_linear,
  314. .is_enabled = max8998_ldo_is_enabled,
  315. .enable = max8998_ldo_enable,
  316. .disable = max8998_ldo_disable,
  317. .get_voltage_sel = max8998_get_voltage_sel,
  318. .set_voltage_sel = max8998_set_voltage_ldo_sel,
  319. };
  320. static const struct regulator_ops max8998_buck_ops = {
  321. .list_voltage = regulator_list_voltage_linear,
  322. .map_voltage = regulator_map_voltage_linear,
  323. .is_enabled = max8998_ldo_is_enabled,
  324. .enable = max8998_ldo_enable,
  325. .disable = max8998_ldo_disable,
  326. .get_voltage_sel = max8998_get_voltage_sel,
  327. .set_voltage_sel = max8998_set_voltage_buck_sel,
  328. .set_voltage_time_sel = max8998_set_voltage_buck_time_sel,
  329. };
  330. static const struct regulator_ops max8998_others_ops = {
  331. .is_enabled = max8998_ldo_is_enabled,
  332. .enable = max8998_ldo_enable,
  333. .disable = max8998_ldo_disable,
  334. };
  335. #define MAX8998_LINEAR_REG(_name, _ops, _min, _step, _max) \
  336. { \
  337. .name = #_name, \
  338. .id = MAX8998_##_name, \
  339. .ops = _ops, \
  340. .min_uV = (_min), \
  341. .uV_step = (_step), \
  342. .n_voltages = ((_max) - (_min)) / (_step) + 1, \
  343. .type = REGULATOR_VOLTAGE, \
  344. .owner = THIS_MODULE, \
  345. }
  346. #define MAX8998_OTHERS_REG(_name, _id) \
  347. { \
  348. .name = #_name, \
  349. .id = _id, \
  350. .ops = &max8998_others_ops, \
  351. .type = REGULATOR_VOLTAGE, \
  352. .owner = THIS_MODULE, \
  353. }
  354. static const struct regulator_desc regulators[] = {
  355. MAX8998_LINEAR_REG(LDO2, &max8998_ldo_ops, 800000, 50000, 1300000),
  356. MAX8998_LINEAR_REG(LDO3, &max8998_ldo_ops, 800000, 50000, 1300000),
  357. MAX8998_LINEAR_REG(LDO4, &max8998_ldo_ops, 1600000, 100000, 3600000),
  358. MAX8998_LINEAR_REG(LDO5, &max8998_ldo_ops, 1600000, 100000, 3600000),
  359. MAX8998_LINEAR_REG(LDO6, &max8998_ldo_ops, 1600000, 100000, 3600000),
  360. MAX8998_LINEAR_REG(LDO7, &max8998_ldo_ops, 1600000, 100000, 3600000),
  361. MAX8998_LINEAR_REG(LDO8, &max8998_ldo_ops, 3000000, 100000, 3600000),
  362. MAX8998_LINEAR_REG(LDO9, &max8998_ldo_ops, 2800000, 100000, 3100000),
  363. MAX8998_LINEAR_REG(LDO10, &max8998_ldo_ops, 950000, 50000, 1300000),
  364. MAX8998_LINEAR_REG(LDO11, &max8998_ldo_ops, 1600000, 100000, 3600000),
  365. MAX8998_LINEAR_REG(LDO12, &max8998_ldo_ops, 800000, 100000, 3300000),
  366. MAX8998_LINEAR_REG(LDO13, &max8998_ldo_ops, 800000, 100000, 3300000),
  367. MAX8998_LINEAR_REG(LDO14, &max8998_ldo_ops, 1200000, 100000, 3300000),
  368. MAX8998_LINEAR_REG(LDO15, &max8998_ldo_ops, 1200000, 100000, 3300000),
  369. MAX8998_LINEAR_REG(LDO16, &max8998_ldo_ops, 1600000, 100000, 3600000),
  370. MAX8998_LINEAR_REG(LDO17, &max8998_ldo_ops, 1600000, 100000, 3600000),
  371. MAX8998_LINEAR_REG(BUCK1, &max8998_buck_ops, 750000, 25000, 1525000),
  372. MAX8998_LINEAR_REG(BUCK2, &max8998_buck_ops, 750000, 25000, 1525000),
  373. MAX8998_LINEAR_REG(BUCK3, &max8998_buck_ops, 1600000, 100000, 3600000),
  374. MAX8998_LINEAR_REG(BUCK4, &max8998_buck_ops, 800000, 100000, 2300000),
  375. MAX8998_OTHERS_REG(EN32KHz-AP, MAX8998_EN32KHZ_AP),
  376. MAX8998_OTHERS_REG(EN32KHz-CP, MAX8998_EN32KHZ_CP),
  377. MAX8998_OTHERS_REG(ENVICHG, MAX8998_ENVICHG),
  378. MAX8998_OTHERS_REG(ESAFEOUT1, MAX8998_ESAFEOUT1),
  379. MAX8998_OTHERS_REG(ESAFEOUT2, MAX8998_ESAFEOUT2),
  380. };
  381. static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
  382. struct max8998_platform_data *pdata,
  383. struct device_node *pmic_np)
  384. {
  385. int gpio;
  386. gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
  387. if (!gpio_is_valid(gpio)) {
  388. dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
  389. return -EINVAL;
  390. }
  391. pdata->buck1_set1 = gpio;
  392. gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
  393. if (!gpio_is_valid(gpio)) {
  394. dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
  395. return -EINVAL;
  396. }
  397. pdata->buck1_set2 = gpio;
  398. gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
  399. if (!gpio_is_valid(gpio)) {
  400. dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
  401. return -EINVAL;
  402. }
  403. pdata->buck2_set3 = gpio;
  404. return 0;
  405. }
  406. static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
  407. struct max8998_platform_data *pdata)
  408. {
  409. struct device_node *pmic_np = iodev->dev->of_node;
  410. struct device_node *regulators_np, *reg_np;
  411. struct max8998_regulator_data *rdata;
  412. unsigned int i;
  413. int ret;
  414. regulators_np = of_get_child_by_name(pmic_np, "regulators");
  415. if (!regulators_np) {
  416. dev_err(iodev->dev, "could not find regulators sub-node\n");
  417. return -EINVAL;
  418. }
  419. /* count the number of regulators to be supported in pmic */
  420. pdata->num_regulators = of_get_child_count(regulators_np);
  421. rdata = devm_kcalloc(iodev->dev,
  422. pdata->num_regulators, sizeof(*rdata),
  423. GFP_KERNEL);
  424. if (!rdata) {
  425. of_node_put(regulators_np);
  426. return -ENOMEM;
  427. }
  428. pdata->regulators = rdata;
  429. for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
  430. reg_np = of_get_child_by_name(regulators_np,
  431. regulators[i].name);
  432. if (!reg_np)
  433. continue;
  434. rdata->id = regulators[i].id;
  435. rdata->initdata = of_get_regulator_init_data(iodev->dev,
  436. reg_np,
  437. &regulators[i]);
  438. rdata->reg_node = reg_np;
  439. ++rdata;
  440. }
  441. pdata->num_regulators = rdata - pdata->regulators;
  442. of_node_put(reg_np);
  443. of_node_put(regulators_np);
  444. ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
  445. if (ret)
  446. return -EINVAL;
  447. if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
  448. pdata->buck_voltage_lock = true;
  449. ret = of_property_read_u32(pmic_np,
  450. "max8998,pmic-buck1-default-dvs-idx",
  451. &pdata->buck1_default_idx);
  452. if (!ret && pdata->buck1_default_idx >= 4) {
  453. pdata->buck1_default_idx = 0;
  454. dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
  455. }
  456. ret = of_property_read_u32(pmic_np,
  457. "max8998,pmic-buck2-default-dvs-idx",
  458. &pdata->buck2_default_idx);
  459. if (!ret && pdata->buck2_default_idx >= 2) {
  460. pdata->buck2_default_idx = 0;
  461. dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
  462. }
  463. ret = of_property_read_u32_array(pmic_np,
  464. "max8998,pmic-buck1-dvs-voltage",
  465. pdata->buck1_voltage,
  466. ARRAY_SIZE(pdata->buck1_voltage));
  467. if (ret) {
  468. dev_err(iodev->dev, "buck1 voltages not specified\n");
  469. return -EINVAL;
  470. }
  471. ret = of_property_read_u32_array(pmic_np,
  472. "max8998,pmic-buck2-dvs-voltage",
  473. pdata->buck2_voltage,
  474. ARRAY_SIZE(pdata->buck2_voltage));
  475. if (ret) {
  476. dev_err(iodev->dev, "buck2 voltages not specified\n");
  477. return -EINVAL;
  478. }
  479. return 0;
  480. }
  481. static int max8998_pmic_probe(struct platform_device *pdev)
  482. {
  483. struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
  484. struct max8998_platform_data *pdata = iodev->pdata;
  485. struct regulator_config config = { };
  486. struct regulator_dev *rdev;
  487. struct max8998_data *max8998;
  488. struct i2c_client *i2c;
  489. int i, ret;
  490. unsigned int v;
  491. if (!pdata) {
  492. dev_err(pdev->dev.parent, "No platform init data supplied\n");
  493. return -ENODEV;
  494. }
  495. if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
  496. ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
  497. if (ret)
  498. return ret;
  499. }
  500. max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
  501. GFP_KERNEL);
  502. if (!max8998)
  503. return -ENOMEM;
  504. max8998->dev = &pdev->dev;
  505. max8998->iodev = iodev;
  506. max8998->num_regulators = pdata->num_regulators;
  507. platform_set_drvdata(pdev, max8998);
  508. i2c = max8998->iodev->i2c;
  509. max8998->buck1_idx = pdata->buck1_default_idx;
  510. max8998->buck2_idx = pdata->buck2_default_idx;
  511. /* NOTE: */
  512. /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
  513. /* will be displayed */
  514. /* Check if MAX8998 voltage selection GPIOs are defined */
  515. if (gpio_is_valid(pdata->buck1_set1) &&
  516. gpio_is_valid(pdata->buck1_set2)) {
  517. /* Check if SET1 is not equal to 0 */
  518. if (!pdata->buck1_set1) {
  519. dev_err(&pdev->dev,
  520. "MAX8998 SET1 GPIO defined as 0 !\n");
  521. WARN_ON(!pdata->buck1_set1);
  522. return -EIO;
  523. }
  524. /* Check if SET2 is not equal to 0 */
  525. if (!pdata->buck1_set2) {
  526. dev_err(&pdev->dev,
  527. "MAX8998 SET2 GPIO defined as 0 !\n");
  528. WARN_ON(!pdata->buck1_set2);
  529. return -EIO;
  530. }
  531. gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
  532. gpio_direction_output(pdata->buck1_set1,
  533. max8998->buck1_idx & 0x1);
  534. gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
  535. gpio_direction_output(pdata->buck1_set2,
  536. (max8998->buck1_idx >> 1) & 0x1);
  537. /* Set predefined values for BUCK1 registers */
  538. for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
  539. int index = MAX8998_BUCK1 - MAX8998_LDO2;
  540. i = 0;
  541. while (regulators[index].min_uV +
  542. regulators[index].uV_step * i
  543. < pdata->buck1_voltage[v])
  544. i++;
  545. max8998->buck1_vol[v] = i;
  546. ret = max8998_write_reg(i2c,
  547. MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
  548. if (ret)
  549. return ret;
  550. }
  551. }
  552. if (gpio_is_valid(pdata->buck2_set3)) {
  553. /* Check if SET3 is not equal to 0 */
  554. if (!pdata->buck2_set3) {
  555. dev_err(&pdev->dev,
  556. "MAX8998 SET3 GPIO defined as 0 !\n");
  557. WARN_ON(!pdata->buck2_set3);
  558. return -EIO;
  559. }
  560. gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
  561. gpio_direction_output(pdata->buck2_set3,
  562. max8998->buck2_idx & 0x1);
  563. /* Set predefined values for BUCK2 registers */
  564. for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
  565. int index = MAX8998_BUCK2 - MAX8998_LDO2;
  566. i = 0;
  567. while (regulators[index].min_uV +
  568. regulators[index].uV_step * i
  569. < pdata->buck2_voltage[v])
  570. i++;
  571. max8998->buck2_vol[v] = i;
  572. ret = max8998_write_reg(i2c,
  573. MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
  574. if (ret)
  575. return ret;
  576. }
  577. }
  578. for (i = 0; i < pdata->num_regulators; i++) {
  579. int index = pdata->regulators[i].id - MAX8998_LDO2;
  580. config.dev = max8998->dev;
  581. config.of_node = pdata->regulators[i].reg_node;
  582. config.init_data = pdata->regulators[i].initdata;
  583. config.driver_data = max8998;
  584. rdev = devm_regulator_register(&pdev->dev, &regulators[index],
  585. &config);
  586. if (IS_ERR(rdev)) {
  587. ret = PTR_ERR(rdev);
  588. dev_err(max8998->dev, "regulator %s init failed (%d)\n",
  589. regulators[index].name, ret);
  590. return ret;
  591. }
  592. }
  593. return 0;
  594. }
  595. static const struct platform_device_id max8998_pmic_id[] = {
  596. { "max8998-pmic", TYPE_MAX8998 },
  597. { "lp3974-pmic", TYPE_LP3974 },
  598. { }
  599. };
  600. MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
  601. static struct platform_driver max8998_pmic_driver = {
  602. .driver = {
  603. .name = "max8998-pmic",
  604. },
  605. .probe = max8998_pmic_probe,
  606. .id_table = max8998_pmic_id,
  607. };
  608. static int __init max8998_pmic_init(void)
  609. {
  610. return platform_driver_register(&max8998_pmic_driver);
  611. }
  612. subsys_initcall(max8998_pmic_init);
  613. static void __exit max8998_pmic_cleanup(void)
  614. {
  615. platform_driver_unregister(&max8998_pmic_driver);
  616. }
  617. module_exit(max8998_pmic_cleanup);
  618. MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
  619. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  620. MODULE_LICENSE("GPL");