pfuze100-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/err.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/regulator/of_regulator.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regulator/driver.h>
  27. #include <linux/regulator/machine.h>
  28. #include <linux/regulator/pfuze100.h>
  29. #include <linux/i2c.h>
  30. #include <linux/slab.h>
  31. #include <linux/regmap.h>
  32. #define PFUZE_NUMREGS 128
  33. #define PFUZE100_VOL_OFFSET 0
  34. #define PFUZE100_STANDBY_OFFSET 1
  35. #define PFUZE100_MODE_OFFSET 3
  36. #define PFUZE100_CONF_OFFSET 4
  37. #define PFUZE100_DEVICEID 0x0
  38. #define PFUZE100_REVID 0x3
  39. #define PFUZE100_FABID 0x3
  40. #define PFUZE100_SW1ABVOL 0x20
  41. #define PFUZE100_SW1CVOL 0x2e
  42. #define PFUZE100_SW2VOL 0x35
  43. #define PFUZE100_SW3AVOL 0x3c
  44. #define PFUZE100_SW3BVOL 0x43
  45. #define PFUZE100_SW4VOL 0x4a
  46. #define PFUZE100_SWBSTCON1 0x66
  47. #define PFUZE100_VREFDDRCON 0x6a
  48. #define PFUZE100_VSNVSVOL 0x6b
  49. #define PFUZE100_VGEN1VOL 0x6c
  50. #define PFUZE100_VGEN2VOL 0x6d
  51. #define PFUZE100_VGEN3VOL 0x6e
  52. #define PFUZE100_VGEN4VOL 0x6f
  53. #define PFUZE100_VGEN5VOL 0x70
  54. #define PFUZE100_VGEN6VOL 0x71
  55. struct pfuze_regulator {
  56. struct regulator_desc desc;
  57. unsigned char stby_reg;
  58. unsigned char stby_mask;
  59. };
  60. struct pfuze_chip {
  61. struct regmap *regmap;
  62. struct device *dev;
  63. struct pfuze_regulator regulator_descs[PFUZE100_MAX_REGULATOR];
  64. struct regulator_dev *regulators[PFUZE100_MAX_REGULATOR];
  65. };
  66. static const int pfuze100_swbst[] = {
  67. 5000000, 5050000, 5100000, 5150000,
  68. };
  69. static const int pfuze100_vsnvs[] = {
  70. 1000000, 1100000, 1200000, 1300000, 1500000, 1800000, 3000000,
  71. };
  72. static const struct i2c_device_id pfuze_device_id[] = {
  73. {.name = "pfuze100"},
  74. {},
  75. };
  76. MODULE_DEVICE_TABLE(i2c, pfuze_device_id);
  77. static const struct of_device_id pfuze_dt_ids[] = {
  78. { .compatible = "fsl,pfuze100" },
  79. {},
  80. };
  81. MODULE_DEVICE_TABLE(of, pfuze_dt_ids);
  82. static int pfuze100_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  83. {
  84. struct pfuze_chip *pfuze100 = rdev_get_drvdata(rdev);
  85. int id = rdev->desc->id;
  86. unsigned int val, ramp_bits, reg;
  87. int ret;
  88. if (id < PFUZE100_SWBST) {
  89. if (id == PFUZE100_SW1AB)
  90. reg = PFUZE100_SW1ABVOL;
  91. else
  92. reg = PFUZE100_SW1CVOL + (id - PFUZE100_SW1C) * 7;
  93. regmap_read(pfuze100->regmap, reg, &val);
  94. if (id <= PFUZE100_SW1C)
  95. ramp_delay = 25000 / (2 * ramp_delay);
  96. else if (val & 0x40)
  97. ramp_delay = 50000 / (4 * ramp_delay);
  98. else
  99. ramp_delay = 25000 / (2 * ramp_delay);
  100. ramp_bits = (ramp_delay >> 1) - (ramp_delay >> 3);
  101. ret = regmap_update_bits(pfuze100->regmap, reg + 4 , 0xc0,
  102. ramp_bits << 6);
  103. if (ret < 0)
  104. dev_err(pfuze100->dev, "ramp failed, err %d\n", ret);
  105. } else
  106. ret = -EACCES;
  107. return ret;
  108. }
  109. static struct regulator_ops pfuze100_ldo_regulator_ops = {
  110. .enable = regulator_enable_regmap,
  111. .disable = regulator_disable_regmap,
  112. .is_enabled = regulator_is_enabled_regmap,
  113. .list_voltage = regulator_list_voltage_linear,
  114. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  115. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  116. };
  117. static struct regulator_ops pfuze100_fixed_regulator_ops = {
  118. .list_voltage = regulator_list_voltage_linear,
  119. };
  120. static struct regulator_ops pfuze100_sw_regulator_ops = {
  121. .list_voltage = regulator_list_voltage_linear,
  122. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  123. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  124. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  125. .set_ramp_delay = pfuze100_set_ramp_delay,
  126. };
  127. static struct regulator_ops pfuze100_swb_regulator_ops = {
  128. .list_voltage = regulator_list_voltage_table,
  129. .map_voltage = regulator_map_voltage_ascend,
  130. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  131. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  132. };
  133. #define PFUZE100_FIXED_REG(_name, base, voltage) \
  134. [PFUZE100_ ## _name] = { \
  135. .desc = { \
  136. .name = #_name, \
  137. .n_voltages = 1, \
  138. .ops = &pfuze100_fixed_regulator_ops, \
  139. .type = REGULATOR_VOLTAGE, \
  140. .id = PFUZE100_ ## _name, \
  141. .owner = THIS_MODULE, \
  142. .min_uV = (voltage), \
  143. .enable_reg = (base), \
  144. .enable_mask = 0x10, \
  145. }, \
  146. }
  147. #define PFUZE100_SW_REG(_name, base, min, max, step) \
  148. [PFUZE100_ ## _name] = { \
  149. .desc = { \
  150. .name = #_name,\
  151. .n_voltages = ((max) - (min)) / (step) + 1, \
  152. .ops = &pfuze100_sw_regulator_ops, \
  153. .type = REGULATOR_VOLTAGE, \
  154. .id = PFUZE100_ ## _name, \
  155. .owner = THIS_MODULE, \
  156. .min_uV = (min), \
  157. .uV_step = (step), \
  158. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  159. .vsel_mask = 0x3f, \
  160. }, \
  161. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  162. .stby_mask = 0x3f, \
  163. }
  164. #define PFUZE100_SWB_REG(_name, base, mask, voltages) \
  165. [PFUZE100_ ## _name] = { \
  166. .desc = { \
  167. .name = #_name, \
  168. .n_voltages = ARRAY_SIZE(voltages), \
  169. .ops = &pfuze100_swb_regulator_ops, \
  170. .type = REGULATOR_VOLTAGE, \
  171. .id = PFUZE100_ ## _name, \
  172. .owner = THIS_MODULE, \
  173. .volt_table = voltages, \
  174. .vsel_reg = (base), \
  175. .vsel_mask = (mask), \
  176. }, \
  177. }
  178. #define PFUZE100_VGEN_REG(_name, base, min, max, step) \
  179. [PFUZE100_ ## _name] = { \
  180. .desc = { \
  181. .name = #_name, \
  182. .n_voltages = ((max) - (min)) / (step) + 1, \
  183. .ops = &pfuze100_ldo_regulator_ops, \
  184. .type = REGULATOR_VOLTAGE, \
  185. .id = PFUZE100_ ## _name, \
  186. .owner = THIS_MODULE, \
  187. .min_uV = (min), \
  188. .uV_step = (step), \
  189. .vsel_reg = (base), \
  190. .vsel_mask = 0xf, \
  191. .enable_reg = (base), \
  192. .enable_mask = 0x10, \
  193. }, \
  194. .stby_reg = (base), \
  195. .stby_mask = 0x20, \
  196. }
  197. static struct pfuze_regulator pfuze100_regulators[] = {
  198. PFUZE100_SW_REG(SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  199. PFUZE100_SW_REG(SW1C, PFUZE100_SW1CVOL, 300000, 1875000, 25000),
  200. PFUZE100_SW_REG(SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  201. PFUZE100_SW_REG(SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  202. PFUZE100_SW_REG(SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  203. PFUZE100_SW_REG(SW4, PFUZE100_SW4VOL, 400000, 1975000, 25000),
  204. PFUZE100_SWB_REG(SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  205. PFUZE100_SWB_REG(VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  206. PFUZE100_FIXED_REG(VREFDDR, PFUZE100_VREFDDRCON, 750000),
  207. PFUZE100_VGEN_REG(VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  208. PFUZE100_VGEN_REG(VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  209. PFUZE100_VGEN_REG(VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  210. PFUZE100_VGEN_REG(VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  211. PFUZE100_VGEN_REG(VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  212. PFUZE100_VGEN_REG(VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  213. };
  214. #ifdef CONFIG_OF
  215. static struct of_regulator_match pfuze100_matches[] = {
  216. { .name = "sw1ab", },
  217. { .name = "sw1c", },
  218. { .name = "sw2", },
  219. { .name = "sw3a", },
  220. { .name = "sw3b", },
  221. { .name = "sw4", },
  222. { .name = "swbst", },
  223. { .name = "vsnvs", },
  224. { .name = "vrefddr", },
  225. { .name = "vgen1", },
  226. { .name = "vgen2", },
  227. { .name = "vgen3", },
  228. { .name = "vgen4", },
  229. { .name = "vgen5", },
  230. { .name = "vgen6", },
  231. };
  232. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  233. {
  234. struct device *dev = chip->dev;
  235. struct device_node *np, *parent;
  236. int ret;
  237. np = of_node_get(dev->parent->of_node);
  238. if (!np)
  239. return 0;
  240. parent = of_find_node_by_name(np, "regulators");
  241. if (!parent) {
  242. dev_err(dev, "regulators node not found\n");
  243. return -EINVAL;
  244. }
  245. ret = of_regulator_match(dev, parent, pfuze100_matches,
  246. ARRAY_SIZE(pfuze100_matches));
  247. of_node_put(parent);
  248. if (ret < 0) {
  249. dev_err(dev, "Error parsing regulator init data: %d\n",
  250. ret);
  251. return ret;
  252. }
  253. return 0;
  254. }
  255. static inline struct regulator_init_data *match_init_data(int index)
  256. {
  257. return pfuze100_matches[index].init_data;
  258. }
  259. static inline struct device_node *match_of_node(int index)
  260. {
  261. return pfuze100_matches[index].of_node;
  262. }
  263. #else
  264. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  265. {
  266. return 0;
  267. }
  268. static inline struct regulator_init_data *match_init_data(int index)
  269. {
  270. return NULL;
  271. }
  272. static inline struct device_node *match_of_node(int index)
  273. {
  274. return NULL;
  275. }
  276. #endif
  277. static int pfuze_identify(struct pfuze_chip *pfuze_chip)
  278. {
  279. unsigned int value;
  280. int ret;
  281. ret = regmap_read(pfuze_chip->regmap, PFUZE100_DEVICEID, &value);
  282. if (ret)
  283. return ret;
  284. if (value & 0x0f) {
  285. dev_warn(pfuze_chip->dev, "Illegal ID: %x\n", value);
  286. return -ENODEV;
  287. }
  288. ret = regmap_read(pfuze_chip->regmap, PFUZE100_REVID, &value);
  289. if (ret)
  290. return ret;
  291. dev_info(pfuze_chip->dev,
  292. "Full lay: %x, Metal lay: %x\n",
  293. (value & 0xf0) >> 4, value & 0x0f);
  294. ret = regmap_read(pfuze_chip->regmap, PFUZE100_FABID, &value);
  295. if (ret)
  296. return ret;
  297. dev_info(pfuze_chip->dev, "FAB: %x, FIN: %x\n",
  298. (value & 0xc) >> 2, value & 0x3);
  299. return 0;
  300. }
  301. static const struct regmap_config pfuze_regmap_config = {
  302. .reg_bits = 8,
  303. .val_bits = 8,
  304. .max_register = PFUZE_NUMREGS,
  305. .cache_type = REGCACHE_RBTREE,
  306. };
  307. static int pfuze100_regulator_probe(struct i2c_client *client,
  308. const struct i2c_device_id *id)
  309. {
  310. struct pfuze_chip *pfuze_chip;
  311. struct pfuze_regulator_platform_data *pdata =
  312. dev_get_platdata(&client->dev);
  313. struct regulator_config config = { };
  314. int i, ret;
  315. pfuze_chip = devm_kzalloc(&client->dev, sizeof(*pfuze_chip),
  316. GFP_KERNEL);
  317. if (!pfuze_chip)
  318. return -ENOMEM;
  319. i2c_set_clientdata(client, pfuze_chip);
  320. memcpy(pfuze_chip->regulator_descs, pfuze100_regulators,
  321. sizeof(pfuze_chip->regulator_descs));
  322. pfuze_chip->dev = &client->dev;
  323. pfuze_chip->regmap = devm_regmap_init_i2c(client, &pfuze_regmap_config);
  324. if (IS_ERR(pfuze_chip->regmap)) {
  325. ret = PTR_ERR(pfuze_chip->regmap);
  326. dev_err(&client->dev,
  327. "regmap allocation failed with err %d\n", ret);
  328. return ret;
  329. }
  330. ret = pfuze_identify(pfuze_chip);
  331. if (ret) {
  332. dev_err(&client->dev, "unrecognized pfuze chip ID!\n");
  333. return ret;
  334. }
  335. ret = pfuze_parse_regulators_dt(pfuze_chip);
  336. if (ret)
  337. return ret;
  338. for (i = 0; i < PFUZE100_MAX_REGULATOR; i++) {
  339. struct regulator_init_data *init_data;
  340. int val;
  341. if (pdata)
  342. init_data = pdata->init_data[i];
  343. else
  344. init_data = match_init_data(i);
  345. /* SW2~SW4 high bit check and modify the voltage value table */
  346. if (i > PFUZE100_SW1C && i < PFUZE100_SWBST) {
  347. regmap_read(pfuze_chip->regmap, PFUZE100_SW2VOL +
  348. (i - PFUZE100_SW2) * 7, &val);
  349. if (val & 0x40) {
  350. pfuze_chip->regulator_descs[i].desc.min_uV
  351. = 800000;
  352. pfuze_chip->regulator_descs[i].desc.uV_step
  353. = 50000;
  354. }
  355. }
  356. config.dev = &client->dev;
  357. config.init_data = init_data;
  358. config.driver_data = pfuze_chip;
  359. config.of_node = match_of_node(i);
  360. pfuze_chip->regulators[i] = regulator_register(&pfuze_chip
  361. ->regulator_descs[i].desc, &config);
  362. if (IS_ERR(pfuze_chip->regulators[i])) {
  363. dev_err(&client->dev, "register regulator%s failed\n",
  364. pfuze100_regulators[i].desc.name);
  365. ret = PTR_ERR(pfuze_chip->regulators[i]);
  366. while (--i >= 0)
  367. regulator_unregister(pfuze_chip->regulators[i]);
  368. return ret;
  369. }
  370. }
  371. return 0;
  372. }
  373. static int pfuze100_regulator_remove(struct i2c_client *client)
  374. {
  375. int i;
  376. struct pfuze_chip *pfuze_chip = i2c_get_clientdata(client);
  377. for (i = 0; i < PFUZE100_MAX_REGULATOR; i++)
  378. regulator_unregister(pfuze_chip->regulators[i]);
  379. return 0;
  380. }
  381. static struct i2c_driver pfuze_driver = {
  382. .id_table = pfuze_device_id,
  383. .driver = {
  384. .name = "pfuze100-regulator",
  385. .owner = THIS_MODULE,
  386. .of_match_table = pfuze_dt_ids,
  387. },
  388. .probe = pfuze100_regulator_probe,
  389. .remove = pfuze100_regulator_remove,
  390. };
  391. module_i2c_driver(pfuze_driver);
  392. MODULE_AUTHOR("Robin Gong <b38343@freescale.com>");
  393. MODULE_DESCRIPTION("Regulator Driver for Freescale PFUZE100 PMIC");
  394. MODULE_LICENSE("GPL v2");
  395. MODULE_ALIAS("i2c:pfuze100-regulator");