pfuze100-regulator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 0x4
  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. enum chips { PFUZE100, PFUZE200, PFUZE3000 = 3 };
  56. struct pfuze_regulator {
  57. struct regulator_desc desc;
  58. unsigned char stby_reg;
  59. unsigned char stby_mask;
  60. };
  61. struct pfuze_chip {
  62. int chip_id;
  63. struct regmap *regmap;
  64. struct device *dev;
  65. struct pfuze_regulator regulator_descs[PFUZE100_MAX_REGULATOR];
  66. struct regulator_dev *regulators[PFUZE100_MAX_REGULATOR];
  67. };
  68. static const int pfuze100_swbst[] = {
  69. 5000000, 5050000, 5100000, 5150000,
  70. };
  71. static const int pfuze100_vsnvs[] = {
  72. 1000000, 1100000, 1200000, 1300000, 1500000, 1800000, 3000000,
  73. };
  74. static const int pfuze3000_sw2lo[] = {
  75. 1500000, 1550000, 1600000, 1650000, 1700000, 1750000, 1800000, 1850000,
  76. };
  77. static const int pfuze3000_sw2hi[] = {
  78. 2500000, 2800000, 2850000, 3000000, 3100000, 3150000, 3200000, 3300000,
  79. };
  80. static const struct i2c_device_id pfuze_device_id[] = {
  81. {.name = "pfuze100", .driver_data = PFUZE100},
  82. {.name = "pfuze200", .driver_data = PFUZE200},
  83. {.name = "pfuze3000", .driver_data = PFUZE3000},
  84. { }
  85. };
  86. MODULE_DEVICE_TABLE(i2c, pfuze_device_id);
  87. static const struct of_device_id pfuze_dt_ids[] = {
  88. { .compatible = "fsl,pfuze100", .data = (void *)PFUZE100},
  89. { .compatible = "fsl,pfuze200", .data = (void *)PFUZE200},
  90. { .compatible = "fsl,pfuze3000", .data = (void *)PFUZE3000},
  91. { }
  92. };
  93. MODULE_DEVICE_TABLE(of, pfuze_dt_ids);
  94. static int pfuze100_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  95. {
  96. struct pfuze_chip *pfuze100 = rdev_get_drvdata(rdev);
  97. int id = rdev_get_id(rdev);
  98. unsigned int ramp_bits;
  99. int ret;
  100. if (id < PFUZE100_SWBST) {
  101. ramp_delay = 12500 / ramp_delay;
  102. ramp_bits = (ramp_delay >> 1) - (ramp_delay >> 3);
  103. ret = regmap_update_bits(pfuze100->regmap,
  104. rdev->desc->vsel_reg + 4,
  105. 0xc0, ramp_bits << 6);
  106. if (ret < 0)
  107. dev_err(pfuze100->dev, "ramp failed, err %d\n", ret);
  108. } else
  109. ret = -EACCES;
  110. return ret;
  111. }
  112. static struct regulator_ops pfuze100_ldo_regulator_ops = {
  113. .enable = regulator_enable_regmap,
  114. .disable = regulator_disable_regmap,
  115. .is_enabled = regulator_is_enabled_regmap,
  116. .list_voltage = regulator_list_voltage_linear,
  117. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  118. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  119. };
  120. static struct regulator_ops pfuze100_fixed_regulator_ops = {
  121. .enable = regulator_enable_regmap,
  122. .disable = regulator_disable_regmap,
  123. .is_enabled = regulator_is_enabled_regmap,
  124. .list_voltage = regulator_list_voltage_linear,
  125. };
  126. static struct regulator_ops pfuze100_sw_regulator_ops = {
  127. .list_voltage = regulator_list_voltage_linear,
  128. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  129. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  130. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  131. .set_ramp_delay = pfuze100_set_ramp_delay,
  132. };
  133. static struct regulator_ops pfuze100_swb_regulator_ops = {
  134. .enable = regulator_enable_regmap,
  135. .disable = regulator_disable_regmap,
  136. .list_voltage = regulator_list_voltage_table,
  137. .map_voltage = regulator_map_voltage_ascend,
  138. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  139. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  140. };
  141. #define PFUZE100_FIXED_REG(_chip, _name, base, voltage) \
  142. [_chip ## _ ## _name] = { \
  143. .desc = { \
  144. .name = #_name, \
  145. .n_voltages = 1, \
  146. .ops = &pfuze100_fixed_regulator_ops, \
  147. .type = REGULATOR_VOLTAGE, \
  148. .id = _chip ## _ ## _name, \
  149. .owner = THIS_MODULE, \
  150. .min_uV = (voltage), \
  151. .enable_reg = (base), \
  152. .enable_mask = 0x10, \
  153. }, \
  154. }
  155. #define PFUZE100_SW_REG(_chip, _name, base, min, max, step) \
  156. [_chip ## _ ## _name] = { \
  157. .desc = { \
  158. .name = #_name,\
  159. .n_voltages = ((max) - (min)) / (step) + 1, \
  160. .ops = &pfuze100_sw_regulator_ops, \
  161. .type = REGULATOR_VOLTAGE, \
  162. .id = _chip ## _ ## _name, \
  163. .owner = THIS_MODULE, \
  164. .min_uV = (min), \
  165. .uV_step = (step), \
  166. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  167. .vsel_mask = 0x3f, \
  168. }, \
  169. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  170. .stby_mask = 0x3f, \
  171. }
  172. #define PFUZE100_SWB_REG(_chip, _name, base, mask, voltages) \
  173. [_chip ## _ ## _name] = { \
  174. .desc = { \
  175. .name = #_name, \
  176. .n_voltages = ARRAY_SIZE(voltages), \
  177. .ops = &pfuze100_swb_regulator_ops, \
  178. .type = REGULATOR_VOLTAGE, \
  179. .id = _chip ## _ ## _name, \
  180. .owner = THIS_MODULE, \
  181. .volt_table = voltages, \
  182. .vsel_reg = (base), \
  183. .vsel_mask = (mask), \
  184. .enable_reg = (base), \
  185. .enable_mask = 0x48, \
  186. }, \
  187. }
  188. #define PFUZE100_VGEN_REG(_chip, _name, base, min, max, step) \
  189. [_chip ## _ ## _name] = { \
  190. .desc = { \
  191. .name = #_name, \
  192. .n_voltages = ((max) - (min)) / (step) + 1, \
  193. .ops = &pfuze100_ldo_regulator_ops, \
  194. .type = REGULATOR_VOLTAGE, \
  195. .id = _chip ## _ ## _name, \
  196. .owner = THIS_MODULE, \
  197. .min_uV = (min), \
  198. .uV_step = (step), \
  199. .vsel_reg = (base), \
  200. .vsel_mask = 0xf, \
  201. .enable_reg = (base), \
  202. .enable_mask = 0x10, \
  203. }, \
  204. .stby_reg = (base), \
  205. .stby_mask = 0x20, \
  206. }
  207. #define PFUZE3000_VCC_REG(_chip, _name, base, min, max, step) { \
  208. .desc = { \
  209. .name = #_name, \
  210. .n_voltages = ((max) - (min)) / (step) + 1, \
  211. .ops = &pfuze100_ldo_regulator_ops, \
  212. .type = REGULATOR_VOLTAGE, \
  213. .id = _chip ## _ ## _name, \
  214. .owner = THIS_MODULE, \
  215. .min_uV = (min), \
  216. .uV_step = (step), \
  217. .vsel_reg = (base), \
  218. .vsel_mask = 0x3, \
  219. .enable_reg = (base), \
  220. .enable_mask = 0x10, \
  221. }, \
  222. .stby_reg = (base), \
  223. .stby_mask = 0x20, \
  224. }
  225. #define PFUZE3000_SW2_REG(_chip, _name, base, min, max, step) { \
  226. .desc = { \
  227. .name = #_name,\
  228. .n_voltages = ((max) - (min)) / (step) + 1, \
  229. .ops = &pfuze100_sw_regulator_ops, \
  230. .type = REGULATOR_VOLTAGE, \
  231. .id = _chip ## _ ## _name, \
  232. .owner = THIS_MODULE, \
  233. .min_uV = (min), \
  234. .uV_step = (step), \
  235. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  236. .vsel_mask = 0x7, \
  237. }, \
  238. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  239. .stby_mask = 0x7, \
  240. }
  241. #define PFUZE3000_SW3_REG(_chip, _name, base, min, max, step) { \
  242. .desc = { \
  243. .name = #_name,\
  244. .n_voltages = ((max) - (min)) / (step) + 1, \
  245. .ops = &pfuze100_sw_regulator_ops, \
  246. .type = REGULATOR_VOLTAGE, \
  247. .id = _chip ## _ ## _name, \
  248. .owner = THIS_MODULE, \
  249. .min_uV = (min), \
  250. .uV_step = (step), \
  251. .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \
  252. .vsel_mask = 0xf, \
  253. }, \
  254. .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \
  255. .stby_mask = 0xf, \
  256. }
  257. /* PFUZE100 */
  258. static struct pfuze_regulator pfuze100_regulators[] = {
  259. PFUZE100_SW_REG(PFUZE100, SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  260. PFUZE100_SW_REG(PFUZE100, SW1C, PFUZE100_SW1CVOL, 300000, 1875000, 25000),
  261. PFUZE100_SW_REG(PFUZE100, SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  262. PFUZE100_SW_REG(PFUZE100, SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  263. PFUZE100_SW_REG(PFUZE100, SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  264. PFUZE100_SW_REG(PFUZE100, SW4, PFUZE100_SW4VOL, 400000, 1975000, 25000),
  265. PFUZE100_SWB_REG(PFUZE100, SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  266. PFUZE100_SWB_REG(PFUZE100, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  267. PFUZE100_FIXED_REG(PFUZE100, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  268. PFUZE100_VGEN_REG(PFUZE100, VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  269. PFUZE100_VGEN_REG(PFUZE100, VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  270. PFUZE100_VGEN_REG(PFUZE100, VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  271. PFUZE100_VGEN_REG(PFUZE100, VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  272. PFUZE100_VGEN_REG(PFUZE100, VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  273. PFUZE100_VGEN_REG(PFUZE100, VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  274. };
  275. static struct pfuze_regulator pfuze200_regulators[] = {
  276. PFUZE100_SW_REG(PFUZE200, SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000),
  277. PFUZE100_SW_REG(PFUZE200, SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000),
  278. PFUZE100_SW_REG(PFUZE200, SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000),
  279. PFUZE100_SW_REG(PFUZE200, SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000),
  280. PFUZE100_SWB_REG(PFUZE200, SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst),
  281. PFUZE100_SWB_REG(PFUZE200, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  282. PFUZE100_FIXED_REG(PFUZE200, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  283. PFUZE100_VGEN_REG(PFUZE200, VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000),
  284. PFUZE100_VGEN_REG(PFUZE200, VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  285. PFUZE100_VGEN_REG(PFUZE200, VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000),
  286. PFUZE100_VGEN_REG(PFUZE200, VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000),
  287. PFUZE100_VGEN_REG(PFUZE200, VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  288. PFUZE100_VGEN_REG(PFUZE200, VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  289. };
  290. static struct pfuze_regulator pfuze3000_regulators[] = {
  291. PFUZE100_SW_REG(PFUZE3000, SW1A, PFUZE100_SW1ABVOL, 700000, 1475000, 25000),
  292. PFUZE100_SW_REG(PFUZE3000, SW1B, PFUZE100_SW1CVOL, 700000, 1475000, 25000),
  293. PFUZE100_SWB_REG(PFUZE3000, SW2, PFUZE100_SW2VOL, 0x7, pfuze3000_sw2lo),
  294. PFUZE3000_SW3_REG(PFUZE3000, SW3, PFUZE100_SW3AVOL, 900000, 1650000, 50000),
  295. PFUZE100_SWB_REG(PFUZE3000, SWBST, PFUZE100_SWBSTCON1, 0x3, pfuze100_swbst),
  296. PFUZE100_SWB_REG(PFUZE3000, VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs),
  297. PFUZE100_FIXED_REG(PFUZE3000, VREFDDR, PFUZE100_VREFDDRCON, 750000),
  298. PFUZE100_VGEN_REG(PFUZE3000, VLDO1, PFUZE100_VGEN1VOL, 1800000, 3300000, 100000),
  299. PFUZE100_VGEN_REG(PFUZE3000, VLDO2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000),
  300. PFUZE3000_VCC_REG(PFUZE3000, VCCSD, PFUZE100_VGEN3VOL, 2850000, 3300000, 150000),
  301. PFUZE3000_VCC_REG(PFUZE3000, V33, PFUZE100_VGEN4VOL, 2850000, 3300000, 150000),
  302. PFUZE100_VGEN_REG(PFUZE3000, VLDO3, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000),
  303. PFUZE100_VGEN_REG(PFUZE3000, VLDO4, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000),
  304. };
  305. static struct pfuze_regulator *pfuze_regulators;
  306. #ifdef CONFIG_OF
  307. /* PFUZE100 */
  308. static struct of_regulator_match pfuze100_matches[] = {
  309. { .name = "sw1ab", },
  310. { .name = "sw1c", },
  311. { .name = "sw2", },
  312. { .name = "sw3a", },
  313. { .name = "sw3b", },
  314. { .name = "sw4", },
  315. { .name = "swbst", },
  316. { .name = "vsnvs", },
  317. { .name = "vrefddr", },
  318. { .name = "vgen1", },
  319. { .name = "vgen2", },
  320. { .name = "vgen3", },
  321. { .name = "vgen4", },
  322. { .name = "vgen5", },
  323. { .name = "vgen6", },
  324. };
  325. /* PFUZE200 */
  326. static struct of_regulator_match pfuze200_matches[] = {
  327. { .name = "sw1ab", },
  328. { .name = "sw2", },
  329. { .name = "sw3a", },
  330. { .name = "sw3b", },
  331. { .name = "swbst", },
  332. { .name = "vsnvs", },
  333. { .name = "vrefddr", },
  334. { .name = "vgen1", },
  335. { .name = "vgen2", },
  336. { .name = "vgen3", },
  337. { .name = "vgen4", },
  338. { .name = "vgen5", },
  339. { .name = "vgen6", },
  340. };
  341. /* PFUZE3000 */
  342. static struct of_regulator_match pfuze3000_matches[] = {
  343. { .name = "sw1a", },
  344. { .name = "sw1b", },
  345. { .name = "sw2", },
  346. { .name = "sw3", },
  347. { .name = "swbst", },
  348. { .name = "vsnvs", },
  349. { .name = "vrefddr", },
  350. { .name = "vldo1", },
  351. { .name = "vldo2", },
  352. { .name = "vccsd", },
  353. { .name = "v33", },
  354. { .name = "vldo3", },
  355. { .name = "vldo4", },
  356. };
  357. static struct of_regulator_match *pfuze_matches;
  358. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  359. {
  360. struct device *dev = chip->dev;
  361. struct device_node *np, *parent;
  362. int ret;
  363. np = of_node_get(dev->of_node);
  364. if (!np)
  365. return -EINVAL;
  366. parent = of_get_child_by_name(np, "regulators");
  367. if (!parent) {
  368. dev_err(dev, "regulators node not found\n");
  369. return -EINVAL;
  370. }
  371. switch (chip->chip_id) {
  372. case PFUZE3000:
  373. pfuze_matches = pfuze3000_matches;
  374. ret = of_regulator_match(dev, parent, pfuze3000_matches,
  375. ARRAY_SIZE(pfuze3000_matches));
  376. break;
  377. case PFUZE200:
  378. pfuze_matches = pfuze200_matches;
  379. ret = of_regulator_match(dev, parent, pfuze200_matches,
  380. ARRAY_SIZE(pfuze200_matches));
  381. break;
  382. case PFUZE100:
  383. default:
  384. pfuze_matches = pfuze100_matches;
  385. ret = of_regulator_match(dev, parent, pfuze100_matches,
  386. ARRAY_SIZE(pfuze100_matches));
  387. break;
  388. }
  389. of_node_put(parent);
  390. if (ret < 0) {
  391. dev_err(dev, "Error parsing regulator init data: %d\n",
  392. ret);
  393. return ret;
  394. }
  395. return 0;
  396. }
  397. static inline struct regulator_init_data *match_init_data(int index)
  398. {
  399. return pfuze_matches[index].init_data;
  400. }
  401. static inline struct device_node *match_of_node(int index)
  402. {
  403. return pfuze_matches[index].of_node;
  404. }
  405. #else
  406. static int pfuze_parse_regulators_dt(struct pfuze_chip *chip)
  407. {
  408. return 0;
  409. }
  410. static inline struct regulator_init_data *match_init_data(int index)
  411. {
  412. return NULL;
  413. }
  414. static inline struct device_node *match_of_node(int index)
  415. {
  416. return NULL;
  417. }
  418. #endif
  419. static int pfuze_identify(struct pfuze_chip *pfuze_chip)
  420. {
  421. unsigned int value;
  422. int ret;
  423. ret = regmap_read(pfuze_chip->regmap, PFUZE100_DEVICEID, &value);
  424. if (ret)
  425. return ret;
  426. if (((value & 0x0f) == 0x8) && (pfuze_chip->chip_id == PFUZE100)) {
  427. /*
  428. * Freescale misprogrammed 1-3% of parts prior to week 8 of 2013
  429. * as ID=8 in PFUZE100
  430. */
  431. dev_info(pfuze_chip->dev, "Assuming misprogrammed ID=0x8");
  432. } else if ((value & 0x0f) != pfuze_chip->chip_id &&
  433. (value & 0xf0) >> 4 != pfuze_chip->chip_id) {
  434. /* device id NOT match with your setting */
  435. dev_warn(pfuze_chip->dev, "Illegal ID: %x\n", value);
  436. return -ENODEV;
  437. }
  438. ret = regmap_read(pfuze_chip->regmap, PFUZE100_REVID, &value);
  439. if (ret)
  440. return ret;
  441. dev_info(pfuze_chip->dev,
  442. "Full layer: %x, Metal layer: %x\n",
  443. (value & 0xf0) >> 4, value & 0x0f);
  444. ret = regmap_read(pfuze_chip->regmap, PFUZE100_FABID, &value);
  445. if (ret)
  446. return ret;
  447. dev_info(pfuze_chip->dev, "FAB: %x, FIN: %x\n",
  448. (value & 0xc) >> 2, value & 0x3);
  449. return 0;
  450. }
  451. static const struct regmap_config pfuze_regmap_config = {
  452. .reg_bits = 8,
  453. .val_bits = 8,
  454. .max_register = PFUZE_NUMREGS - 1,
  455. .cache_type = REGCACHE_RBTREE,
  456. };
  457. static int pfuze100_regulator_probe(struct i2c_client *client,
  458. const struct i2c_device_id *id)
  459. {
  460. struct pfuze_chip *pfuze_chip;
  461. struct pfuze_regulator_platform_data *pdata =
  462. dev_get_platdata(&client->dev);
  463. struct regulator_config config = { };
  464. int i, ret;
  465. const struct of_device_id *match;
  466. u32 regulator_num;
  467. u32 sw_check_start, sw_check_end, sw_hi = 0x40;
  468. pfuze_chip = devm_kzalloc(&client->dev, sizeof(*pfuze_chip),
  469. GFP_KERNEL);
  470. if (!pfuze_chip)
  471. return -ENOMEM;
  472. if (client->dev.of_node) {
  473. match = of_match_device(of_match_ptr(pfuze_dt_ids),
  474. &client->dev);
  475. if (!match) {
  476. dev_err(&client->dev, "Error: No device match found\n");
  477. return -ENODEV;
  478. }
  479. pfuze_chip->chip_id = (int)(long)match->data;
  480. } else if (id) {
  481. pfuze_chip->chip_id = id->driver_data;
  482. } else {
  483. dev_err(&client->dev, "No dts match or id table match found\n");
  484. return -ENODEV;
  485. }
  486. i2c_set_clientdata(client, pfuze_chip);
  487. pfuze_chip->dev = &client->dev;
  488. pfuze_chip->regmap = devm_regmap_init_i2c(client, &pfuze_regmap_config);
  489. if (IS_ERR(pfuze_chip->regmap)) {
  490. ret = PTR_ERR(pfuze_chip->regmap);
  491. dev_err(&client->dev,
  492. "regmap allocation failed with err %d\n", ret);
  493. return ret;
  494. }
  495. ret = pfuze_identify(pfuze_chip);
  496. if (ret) {
  497. dev_err(&client->dev, "unrecognized pfuze chip ID!\n");
  498. return ret;
  499. }
  500. /* use the right regulators after identify the right device */
  501. switch (pfuze_chip->chip_id) {
  502. case PFUZE3000:
  503. pfuze_regulators = pfuze3000_regulators;
  504. regulator_num = ARRAY_SIZE(pfuze3000_regulators);
  505. sw_check_start = PFUZE3000_SW2;
  506. sw_check_end = PFUZE3000_SW2;
  507. sw_hi = 1 << 3;
  508. break;
  509. case PFUZE200:
  510. pfuze_regulators = pfuze200_regulators;
  511. regulator_num = ARRAY_SIZE(pfuze200_regulators);
  512. sw_check_start = PFUZE200_SW2;
  513. sw_check_end = PFUZE200_SW3B;
  514. break;
  515. case PFUZE100:
  516. default:
  517. pfuze_regulators = pfuze100_regulators;
  518. regulator_num = ARRAY_SIZE(pfuze100_regulators);
  519. sw_check_start = PFUZE100_SW2;
  520. sw_check_end = PFUZE100_SW4;
  521. break;
  522. }
  523. dev_info(&client->dev, "pfuze%s found.\n",
  524. (pfuze_chip->chip_id == PFUZE100) ? "100" :
  525. ((pfuze_chip->chip_id == PFUZE200) ? "200" : "3000"));
  526. memcpy(pfuze_chip->regulator_descs, pfuze_regulators,
  527. sizeof(pfuze_chip->regulator_descs));
  528. ret = pfuze_parse_regulators_dt(pfuze_chip);
  529. if (ret)
  530. return ret;
  531. for (i = 0; i < regulator_num; i++) {
  532. struct regulator_init_data *init_data;
  533. struct regulator_desc *desc;
  534. int val;
  535. desc = &pfuze_chip->regulator_descs[i].desc;
  536. if (pdata)
  537. init_data = pdata->init_data[i];
  538. else
  539. init_data = match_init_data(i);
  540. /* SW2~SW4 high bit check and modify the voltage value table */
  541. if (i >= sw_check_start && i <= sw_check_end) {
  542. regmap_read(pfuze_chip->regmap, desc->vsel_reg, &val);
  543. if (val & sw_hi) {
  544. if (pfuze_chip->chip_id == PFUZE3000) {
  545. desc->volt_table = pfuze3000_sw2hi;
  546. desc->n_voltages = ARRAY_SIZE(pfuze3000_sw2hi);
  547. } else {
  548. desc->min_uV = 800000;
  549. desc->uV_step = 50000;
  550. desc->n_voltages = 51;
  551. }
  552. }
  553. }
  554. config.dev = &client->dev;
  555. config.init_data = init_data;
  556. config.driver_data = pfuze_chip;
  557. config.of_node = match_of_node(i);
  558. config.ena_gpio = -EINVAL;
  559. pfuze_chip->regulators[i] =
  560. devm_regulator_register(&client->dev, desc, &config);
  561. if (IS_ERR(pfuze_chip->regulators[i])) {
  562. dev_err(&client->dev, "register regulator%s failed\n",
  563. pfuze_regulators[i].desc.name);
  564. return PTR_ERR(pfuze_chip->regulators[i]);
  565. }
  566. }
  567. return 0;
  568. }
  569. static struct i2c_driver pfuze_driver = {
  570. .id_table = pfuze_device_id,
  571. .driver = {
  572. .name = "pfuze100-regulator",
  573. .owner = THIS_MODULE,
  574. .of_match_table = pfuze_dt_ids,
  575. },
  576. .probe = pfuze100_regulator_probe,
  577. };
  578. module_i2c_driver(pfuze_driver);
  579. MODULE_AUTHOR("Robin Gong <b38343@freescale.com>");
  580. MODULE_DESCRIPTION("Regulator Driver for Freescale PFUZE100/PFUZE200 PMIC");
  581. MODULE_LICENSE("GPL v2");
  582. MODULE_ALIAS("i2c:pfuze100-regulator");