pfuze100-regulator.c 20 KB

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