pfuze100-regulator.c 19 KB

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