pfuze100-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 ramp_bits;
  87. int ret;
  88. if (id < PFUZE100_SWBST) {
  89. ramp_delay = 12500 / ramp_delay;
  90. ramp_bits = (ramp_delay >> 1) - (ramp_delay >> 3);
  91. ret = regmap_update_bits(pfuze100->regmap,
  92. rdev->desc->vsel_reg + 4,
  93. 0xc0, ramp_bits << 6);
  94. if (ret < 0)
  95. dev_err(pfuze100->dev, "ramp failed, err %d\n", ret);
  96. } else
  97. ret = -EACCES;
  98. return ret;
  99. }
  100. static struct regulator_ops pfuze100_ldo_regulator_ops = {
  101. .enable = regulator_enable_regmap,
  102. .disable = regulator_disable_regmap,
  103. .is_enabled = regulator_is_enabled_regmap,
  104. .list_voltage = regulator_list_voltage_linear,
  105. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  106. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  107. };
  108. static struct regulator_ops pfuze100_fixed_regulator_ops = {
  109. .list_voltage = regulator_list_voltage_linear,
  110. };
  111. static struct regulator_ops pfuze100_sw_regulator_ops = {
  112. .list_voltage = regulator_list_voltage_linear,
  113. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  114. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  115. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  116. .set_ramp_delay = pfuze100_set_ramp_delay,
  117. };
  118. static struct regulator_ops pfuze100_swb_regulator_ops = {
  119. .list_voltage = regulator_list_voltage_table,
  120. .map_voltage = regulator_map_voltage_ascend,
  121. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  122. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  123. };
  124. #define PFUZE100_FIXED_REG(_name, base, voltage) \
  125. [PFUZE100_ ## _name] = { \
  126. .desc = { \
  127. .name = #_name, \
  128. .n_voltages = 1, \
  129. .ops = &pfuze100_fixed_regulator_ops, \
  130. .type = REGULATOR_VOLTAGE, \
  131. .id = PFUZE100_ ## _name, \
  132. .owner = THIS_MODULE, \
  133. .min_uV = (voltage), \
  134. .enable_reg = (base), \
  135. .enable_mask = 0x10, \
  136. }, \
  137. }
  138. #define PFUZE100_SW_REG(_name, base, min, max, step) \
  139. [PFUZE100_ ## _name] = { \
  140. .desc = { \
  141. .name = #_name,\
  142. .n_voltages = ((max) - (min)) / (step) + 1, \
  143. .ops = &pfuze100_sw_regulator_ops, \
  144. .type = REGULATOR_VOLTAGE, \
  145. .id = PFUZE100_ ## _name, \
  146. .owner = THIS_MODULE, \
  147. .min_uV = (min), \
  148. .uV_step = (step), \
  149. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  150. .vsel_mask = 0x3f, \
  151. }, \
  152. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  153. .stby_mask = 0x3f, \
  154. }
  155. #define PFUZE100_SWB_REG(_name, base, mask, voltages) \
  156. [PFUZE100_ ## _name] = { \
  157. .desc = { \
  158. .name = #_name, \
  159. .n_voltages = ARRAY_SIZE(voltages), \
  160. .ops = &pfuze100_swb_regulator_ops, \
  161. .type = REGULATOR_VOLTAGE, \
  162. .id = PFUZE100_ ## _name, \
  163. .owner = THIS_MODULE, \
  164. .volt_table = voltages, \
  165. .vsel_reg = (base), \
  166. .vsel_mask = (mask), \
  167. }, \
  168. }
  169. #define PFUZE100_VGEN_REG(_name, base, min, max, step) \
  170. [PFUZE100_ ## _name] = { \
  171. .desc = { \
  172. .name = #_name, \
  173. .n_voltages = ((max) - (min)) / (step) + 1, \
  174. .ops = &pfuze100_ldo_regulator_ops, \
  175. .type = REGULATOR_VOLTAGE, \
  176. .id = PFUZE100_ ## _name, \
  177. .owner = THIS_MODULE, \
  178. .min_uV = (min), \
  179. .uV_step = (step), \
  180. .vsel_reg = (base), \
  181. .vsel_mask = 0xf, \
  182. .enable_reg = (base), \
  183. .enable_mask = 0x10, \
  184. }, \
  185. .stby_reg = (base), \
  186. .stby_mask = 0x20, \
  187. }
  188. static struct pfuze_regulator pfuze100_regulators[] = {
  189. PFUZE100_SW_REG(SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  190. PFUZE100_SW_REG(SW1C, PFUZE100_SW1CVOL, 300000, 1875000, 25000),
  191. PFUZE100_SW_REG(SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  192. PFUZE100_SW_REG(SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  193. PFUZE100_SW_REG(SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  194. PFUZE100_SW_REG(SW4, PFUZE100_SW4VOL, 400000, 1975000, 25000),
  195. PFUZE100_SWB_REG(SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  196. PFUZE100_SWB_REG(VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  197. PFUZE100_FIXED_REG(VREFDDR, PFUZE100_VREFDDRCON, 750000),
  198. PFUZE100_VGEN_REG(VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  199. PFUZE100_VGEN_REG(VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  200. PFUZE100_VGEN_REG(VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  201. PFUZE100_VGEN_REG(VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  202. PFUZE100_VGEN_REG(VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  203. PFUZE100_VGEN_REG(VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  204. };
  205. #ifdef CONFIG_OF
  206. static struct of_regulator_match pfuze100_matches[] = {
  207. { .name = "sw1ab", },
  208. { .name = "sw1c", },
  209. { .name = "sw2", },
  210. { .name = "sw3a", },
  211. { .name = "sw3b", },
  212. { .name = "sw4", },
  213. { .name = "swbst", },
  214. { .name = "vsnvs", },
  215. { .name = "vrefddr", },
  216. { .name = "vgen1", },
  217. { .name = "vgen2", },
  218. { .name = "vgen3", },
  219. { .name = "vgen4", },
  220. { .name = "vgen5", },
  221. { .name = "vgen6", },
  222. };
  223. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  224. {
  225. struct device *dev = chip->dev;
  226. struct device_node *np, *parent;
  227. int ret;
  228. np = of_node_get(dev->parent->of_node);
  229. if (!np)
  230. return 0;
  231. parent = of_find_node_by_name(np, "regulators");
  232. if (!parent) {
  233. dev_err(dev, "regulators node not found\n");
  234. return -EINVAL;
  235. }
  236. ret = of_regulator_match(dev, parent, pfuze100_matches,
  237. ARRAY_SIZE(pfuze100_matches));
  238. of_node_put(parent);
  239. if (ret < 0) {
  240. dev_err(dev, "Error parsing regulator init data: %d\n",
  241. ret);
  242. return ret;
  243. }
  244. return 0;
  245. }
  246. static inline struct regulator_init_data *match_init_data(int index)
  247. {
  248. return pfuze100_matches[index].init_data;
  249. }
  250. static inline struct device_node *match_of_node(int index)
  251. {
  252. return pfuze100_matches[index].of_node;
  253. }
  254. #else
  255. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  256. {
  257. return 0;
  258. }
  259. static inline struct regulator_init_data *match_init_data(int index)
  260. {
  261. return NULL;
  262. }
  263. static inline struct device_node *match_of_node(int index)
  264. {
  265. return NULL;
  266. }
  267. #endif
  268. static int pfuze_identify(struct pfuze_chip *pfuze_chip)
  269. {
  270. unsigned int value;
  271. int ret;
  272. ret = regmap_read(pfuze_chip->regmap, PFUZE100_DEVICEID, &value);
  273. if (ret)
  274. return ret;
  275. if (value & 0x0f) {
  276. dev_warn(pfuze_chip->dev, "Illegal ID: %x\n", value);
  277. return -ENODEV;
  278. }
  279. ret = regmap_read(pfuze_chip->regmap, PFUZE100_REVID, &value);
  280. if (ret)
  281. return ret;
  282. dev_info(pfuze_chip->dev,
  283. "Full lay: %x, Metal lay: %x\n",
  284. (value & 0xf0) >> 4, value & 0x0f);
  285. ret = regmap_read(pfuze_chip->regmap, PFUZE100_FABID, &value);
  286. if (ret)
  287. return ret;
  288. dev_info(pfuze_chip->dev, "FAB: %x, FIN: %x\n",
  289. (value & 0xc) >> 2, value & 0x3);
  290. return 0;
  291. }
  292. static const struct regmap_config pfuze_regmap_config = {
  293. .reg_bits = 8,
  294. .val_bits = 8,
  295. .max_register = PFUZE_NUMREGS - 1,
  296. .cache_type = REGCACHE_RBTREE,
  297. };
  298. static int pfuze100_regulator_probe(struct i2c_client *client,
  299. const struct i2c_device_id *id)
  300. {
  301. struct pfuze_chip *pfuze_chip;
  302. struct pfuze_regulator_platform_data *pdata =
  303. dev_get_platdata(&client->dev);
  304. struct regulator_config config = { };
  305. int i, ret;
  306. pfuze_chip = devm_kzalloc(&client->dev, sizeof(*pfuze_chip),
  307. GFP_KERNEL);
  308. if (!pfuze_chip)
  309. return -ENOMEM;
  310. i2c_set_clientdata(client, pfuze_chip);
  311. memcpy(pfuze_chip->regulator_descs, pfuze100_regulators,
  312. sizeof(pfuze_chip->regulator_descs));
  313. pfuze_chip->dev = &client->dev;
  314. pfuze_chip->regmap = devm_regmap_init_i2c(client, &pfuze_regmap_config);
  315. if (IS_ERR(pfuze_chip->regmap)) {
  316. ret = PTR_ERR(pfuze_chip->regmap);
  317. dev_err(&client->dev,
  318. "regmap allocation failed with err %d\n", ret);
  319. return ret;
  320. }
  321. ret = pfuze_identify(pfuze_chip);
  322. if (ret) {
  323. dev_err(&client->dev, "unrecognized pfuze chip ID!\n");
  324. return ret;
  325. }
  326. ret = pfuze_parse_regulators_dt(pfuze_chip);
  327. if (ret)
  328. return ret;
  329. for (i = 0; i < PFUZE100_MAX_REGULATOR; i++) {
  330. struct regulator_init_data *init_data;
  331. struct regulator_desc *desc;
  332. int val;
  333. desc = &pfuze_chip->regulator_descs[i].desc;
  334. if (pdata)
  335. init_data = pdata->init_data[i];
  336. else
  337. init_data = match_init_data(i);
  338. /* SW2~SW4 high bit check and modify the voltage value table */
  339. if (i > PFUZE100_SW1C && i < PFUZE100_SWBST) {
  340. regmap_read(pfuze_chip->regmap, desc->vsel_reg, &val);
  341. if (val & 0x40) {
  342. desc->min_uV = 800000;
  343. desc->uV_step = 50000;
  344. desc->n_voltages = 51;
  345. }
  346. }
  347. config.dev = &client->dev;
  348. config.init_data = init_data;
  349. config.driver_data = pfuze_chip;
  350. config.of_node = match_of_node(i);
  351. pfuze_chip->regulators[i] = regulator_register(desc, &config);
  352. if (IS_ERR(pfuze_chip->regulators[i])) {
  353. dev_err(&client->dev, "register regulator%s failed\n",
  354. pfuze100_regulators[i].desc.name);
  355. ret = PTR_ERR(pfuze_chip->regulators[i]);
  356. while (--i >= 0)
  357. regulator_unregister(pfuze_chip->regulators[i]);
  358. return ret;
  359. }
  360. }
  361. return 0;
  362. }
  363. static int pfuze100_regulator_remove(struct i2c_client *client)
  364. {
  365. int i;
  366. struct pfuze_chip *pfuze_chip = i2c_get_clientdata(client);
  367. for (i = 0; i < PFUZE100_MAX_REGULATOR; i++)
  368. regulator_unregister(pfuze_chip->regulators[i]);
  369. return 0;
  370. }
  371. static struct i2c_driver pfuze_driver = {
  372. .id_table = pfuze_device_id,
  373. .driver = {
  374. .name = "pfuze100-regulator",
  375. .owner = THIS_MODULE,
  376. .of_match_table = pfuze_dt_ids,
  377. },
  378. .probe = pfuze100_regulator_probe,
  379. .remove = pfuze100_regulator_remove,
  380. };
  381. module_i2c_driver(pfuze_driver);
  382. MODULE_AUTHOR("Robin Gong <b38343@freescale.com>");
  383. MODULE_DESCRIPTION("Regulator Driver for Freescale PFUZE100 PMIC");
  384. MODULE_LICENSE("GPL v2");
  385. MODULE_ALIAS("i2c:pfuze100-regulator");