fan53555.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
  3. *
  4. * Supported Part Numbers:
  5. * FAN53555UC00X/01X/03X/04X/05X
  6. *
  7. * Copyright (c) 2012 Marvell Technology Ltd.
  8. * Yunfan Zhang <yfzhang@marvell.com>
  9. *
  10. * This package is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/param.h>
  17. #include <linux/err.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/regulator/of_regulator.h>
  22. #include <linux/of_device.h>
  23. #include <linux/i2c.h>
  24. #include <linux/slab.h>
  25. #include <linux/regmap.h>
  26. #include <linux/regulator/fan53555.h>
  27. /* Voltage setting */
  28. #define FAN53555_VSEL0 0x00
  29. #define FAN53555_VSEL1 0x01
  30. /* Control register */
  31. #define FAN53555_CONTROL 0x02
  32. /* IC Type */
  33. #define FAN53555_ID1 0x03
  34. /* IC mask version */
  35. #define FAN53555_ID2 0x04
  36. /* Monitor register */
  37. #define FAN53555_MONITOR 0x05
  38. /* VSEL bit definitions */
  39. #define VSEL_BUCK_EN (1 << 7)
  40. #define VSEL_MODE (1 << 6)
  41. #define VSEL_NSEL_MASK 0x3F
  42. /* Chip ID and Verison */
  43. #define DIE_ID 0x0F /* ID1 */
  44. #define DIE_REV 0x0F /* ID2 */
  45. /* Control bit definitions */
  46. #define CTL_OUTPUT_DISCHG (1 << 7)
  47. #define CTL_SLEW_MASK (0x7 << 4)
  48. #define CTL_SLEW_SHIFT 4
  49. #define CTL_RESET (1 << 2)
  50. #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */
  51. enum fan53555_vendor {
  52. FAN53555_VENDOR_FAIRCHILD = 0,
  53. FAN53555_VENDOR_SILERGY,
  54. };
  55. /* IC Type */
  56. enum {
  57. FAN53555_CHIP_ID_00 = 0,
  58. FAN53555_CHIP_ID_01,
  59. FAN53555_CHIP_ID_02,
  60. FAN53555_CHIP_ID_03,
  61. FAN53555_CHIP_ID_04,
  62. FAN53555_CHIP_ID_05,
  63. FAN53555_CHIP_ID_08 = 8,
  64. };
  65. /* IC mask revision */
  66. enum {
  67. FAN53555_CHIP_REV_00 = 0x3,
  68. FAN53555_CHIP_REV_13 = 0xf,
  69. };
  70. enum {
  71. SILERGY_SYR82X = 8,
  72. };
  73. struct fan53555_device_info {
  74. enum fan53555_vendor vendor;
  75. struct regmap *regmap;
  76. struct device *dev;
  77. struct regulator_desc desc;
  78. struct regulator_dev *rdev;
  79. struct regulator_init_data *regulator;
  80. /* IC Type and Rev */
  81. int chip_id;
  82. int chip_rev;
  83. /* Voltage setting register */
  84. unsigned int vol_reg;
  85. unsigned int sleep_reg;
  86. /* Voltage range and step(linear) */
  87. unsigned int vsel_min;
  88. unsigned int vsel_step;
  89. /* Voltage slew rate limiting */
  90. unsigned int slew_rate;
  91. /* Sleep voltage cache */
  92. unsigned int sleep_vol_cache;
  93. };
  94. static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  95. {
  96. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  97. int ret;
  98. if (di->sleep_vol_cache == uV)
  99. return 0;
  100. ret = regulator_map_voltage_linear(rdev, uV, uV);
  101. if (ret < 0)
  102. return ret;
  103. ret = regmap_update_bits(di->regmap, di->sleep_reg,
  104. VSEL_NSEL_MASK, ret);
  105. if (ret < 0)
  106. return ret;
  107. /* Cache the sleep voltage setting.
  108. * Might not be the real voltage which is rounded */
  109. di->sleep_vol_cache = uV;
  110. return 0;
  111. }
  112. static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
  113. {
  114. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  115. return regmap_update_bits(di->regmap, di->sleep_reg,
  116. VSEL_BUCK_EN, VSEL_BUCK_EN);
  117. }
  118. static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
  119. {
  120. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  121. return regmap_update_bits(di->regmap, di->sleep_reg,
  122. VSEL_BUCK_EN, 0);
  123. }
  124. static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
  125. {
  126. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  127. switch (mode) {
  128. case REGULATOR_MODE_FAST:
  129. regmap_update_bits(di->regmap, di->vol_reg,
  130. VSEL_MODE, VSEL_MODE);
  131. break;
  132. case REGULATOR_MODE_NORMAL:
  133. regmap_update_bits(di->regmap, di->vol_reg, VSEL_MODE, 0);
  134. break;
  135. default:
  136. return -EINVAL;
  137. }
  138. return 0;
  139. }
  140. static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
  141. {
  142. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  143. unsigned int val;
  144. int ret = 0;
  145. ret = regmap_read(di->regmap, di->vol_reg, &val);
  146. if (ret < 0)
  147. return ret;
  148. if (val & VSEL_MODE)
  149. return REGULATOR_MODE_FAST;
  150. else
  151. return REGULATOR_MODE_NORMAL;
  152. }
  153. static const int slew_rates[] = {
  154. 64000,
  155. 32000,
  156. 16000,
  157. 8000,
  158. 4000,
  159. 2000,
  160. 1000,
  161. 500,
  162. };
  163. static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp)
  164. {
  165. struct fan53555_device_info *di = rdev_get_drvdata(rdev);
  166. int regval = -1, i;
  167. for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
  168. if (ramp <= slew_rates[i])
  169. regval = i;
  170. else
  171. break;
  172. }
  173. if (regval < 0) {
  174. dev_err(di->dev, "unsupported ramp value %d\n", ramp);
  175. return -EINVAL;
  176. }
  177. return regmap_update_bits(di->regmap, FAN53555_CONTROL,
  178. CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
  179. }
  180. static const struct regulator_ops fan53555_regulator_ops = {
  181. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  182. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  183. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  184. .map_voltage = regulator_map_voltage_linear,
  185. .list_voltage = regulator_list_voltage_linear,
  186. .set_suspend_voltage = fan53555_set_suspend_voltage,
  187. .enable = regulator_enable_regmap,
  188. .disable = regulator_disable_regmap,
  189. .is_enabled = regulator_is_enabled_regmap,
  190. .set_mode = fan53555_set_mode,
  191. .get_mode = fan53555_get_mode,
  192. .set_ramp_delay = fan53555_set_ramp,
  193. .set_suspend_enable = fan53555_set_suspend_enable,
  194. .set_suspend_disable = fan53555_set_suspend_disable,
  195. };
  196. static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
  197. {
  198. /* Init voltage range and step */
  199. switch (di->chip_id) {
  200. case FAN53555_CHIP_ID_00:
  201. switch (di->chip_rev) {
  202. case FAN53555_CHIP_REV_00:
  203. di->vsel_min = 600000;
  204. di->vsel_step = 10000;
  205. break;
  206. case FAN53555_CHIP_REV_13:
  207. di->vsel_min = 800000;
  208. di->vsel_step = 10000;
  209. break;
  210. default:
  211. dev_err(di->dev,
  212. "Chip ID %d with rev %d not supported!\n",
  213. di->chip_id, di->chip_rev);
  214. return -EINVAL;
  215. }
  216. break;
  217. case FAN53555_CHIP_ID_01:
  218. case FAN53555_CHIP_ID_03:
  219. case FAN53555_CHIP_ID_05:
  220. case FAN53555_CHIP_ID_08:
  221. di->vsel_min = 600000;
  222. di->vsel_step = 10000;
  223. break;
  224. case FAN53555_CHIP_ID_04:
  225. di->vsel_min = 603000;
  226. di->vsel_step = 12826;
  227. break;
  228. default:
  229. dev_err(di->dev,
  230. "Chip ID %d not supported!\n", di->chip_id);
  231. return -EINVAL;
  232. }
  233. return 0;
  234. }
  235. static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
  236. {
  237. /* Init voltage range and step */
  238. switch (di->chip_id) {
  239. case SILERGY_SYR82X:
  240. di->vsel_min = 712500;
  241. di->vsel_step = 12500;
  242. break;
  243. default:
  244. dev_err(di->dev,
  245. "Chip ID %d not supported!\n", di->chip_id);
  246. return -EINVAL;
  247. }
  248. return 0;
  249. }
  250. /* For 00,01,03,05 options:
  251. * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
  252. * For 04 option:
  253. * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
  254. * */
  255. static int fan53555_device_setup(struct fan53555_device_info *di,
  256. struct fan53555_platform_data *pdata)
  257. {
  258. int ret = 0;
  259. /* Setup voltage control register */
  260. switch (pdata->sleep_vsel_id) {
  261. case FAN53555_VSEL_ID_0:
  262. di->sleep_reg = FAN53555_VSEL0;
  263. di->vol_reg = FAN53555_VSEL1;
  264. break;
  265. case FAN53555_VSEL_ID_1:
  266. di->sleep_reg = FAN53555_VSEL1;
  267. di->vol_reg = FAN53555_VSEL0;
  268. break;
  269. default:
  270. dev_err(di->dev, "Invalid VSEL ID!\n");
  271. return -EINVAL;
  272. }
  273. switch (di->vendor) {
  274. case FAN53555_VENDOR_FAIRCHILD:
  275. ret = fan53555_voltages_setup_fairchild(di);
  276. break;
  277. case FAN53555_VENDOR_SILERGY:
  278. ret = fan53555_voltages_setup_silergy(di);
  279. break;
  280. default:
  281. dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
  282. return -EINVAL;
  283. }
  284. return ret;
  285. }
  286. static int fan53555_regulator_register(struct fan53555_device_info *di,
  287. struct regulator_config *config)
  288. {
  289. struct regulator_desc *rdesc = &di->desc;
  290. rdesc->name = "fan53555-reg";
  291. rdesc->supply_name = "vin";
  292. rdesc->ops = &fan53555_regulator_ops;
  293. rdesc->type = REGULATOR_VOLTAGE;
  294. rdesc->n_voltages = FAN53555_NVOLTAGES;
  295. rdesc->enable_reg = di->vol_reg;
  296. rdesc->enable_mask = VSEL_BUCK_EN;
  297. rdesc->min_uV = di->vsel_min;
  298. rdesc->uV_step = di->vsel_step;
  299. rdesc->vsel_reg = di->vol_reg;
  300. rdesc->vsel_mask = VSEL_NSEL_MASK;
  301. rdesc->owner = THIS_MODULE;
  302. di->rdev = devm_regulator_register(di->dev, &di->desc, config);
  303. return PTR_ERR_OR_ZERO(di->rdev);
  304. }
  305. static const struct regmap_config fan53555_regmap_config = {
  306. .reg_bits = 8,
  307. .val_bits = 8,
  308. };
  309. static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
  310. struct device_node *np,
  311. const struct regulator_desc *desc)
  312. {
  313. struct fan53555_platform_data *pdata;
  314. int ret;
  315. u32 tmp;
  316. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  317. if (!pdata)
  318. return NULL;
  319. pdata->regulator = of_get_regulator_init_data(dev, np, desc);
  320. ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
  321. &tmp);
  322. if (!ret)
  323. pdata->sleep_vsel_id = tmp;
  324. return pdata;
  325. }
  326. static const struct of_device_id fan53555_dt_ids[] = {
  327. {
  328. .compatible = "fcs,fan53555",
  329. .data = (void *)FAN53555_VENDOR_FAIRCHILD
  330. }, {
  331. .compatible = "silergy,syr827",
  332. .data = (void *)FAN53555_VENDOR_SILERGY,
  333. }, {
  334. .compatible = "silergy,syr828",
  335. .data = (void *)FAN53555_VENDOR_SILERGY,
  336. },
  337. { }
  338. };
  339. MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
  340. static int fan53555_regulator_probe(struct i2c_client *client,
  341. const struct i2c_device_id *id)
  342. {
  343. struct device_node *np = client->dev.of_node;
  344. struct fan53555_device_info *di;
  345. struct fan53555_platform_data *pdata;
  346. struct regulator_config config = { };
  347. unsigned int val;
  348. int ret;
  349. di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
  350. GFP_KERNEL);
  351. if (!di)
  352. return -ENOMEM;
  353. pdata = dev_get_platdata(&client->dev);
  354. if (!pdata)
  355. pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
  356. if (!pdata || !pdata->regulator) {
  357. dev_err(&client->dev, "Platform data not found!\n");
  358. return -ENODEV;
  359. }
  360. di->regulator = pdata->regulator;
  361. if (client->dev.of_node) {
  362. di->vendor =
  363. (unsigned long)of_device_get_match_data(&client->dev);
  364. } else {
  365. /* if no ramp constraint set, get the pdata ramp_delay */
  366. if (!di->regulator->constraints.ramp_delay) {
  367. int slew_idx = (pdata->slew_rate & 0x7)
  368. ? pdata->slew_rate : 0;
  369. di->regulator->constraints.ramp_delay
  370. = slew_rates[slew_idx];
  371. }
  372. di->vendor = id->driver_data;
  373. }
  374. di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
  375. if (IS_ERR(di->regmap)) {
  376. dev_err(&client->dev, "Failed to allocate regmap!\n");
  377. return PTR_ERR(di->regmap);
  378. }
  379. di->dev = &client->dev;
  380. i2c_set_clientdata(client, di);
  381. /* Get chip ID */
  382. ret = regmap_read(di->regmap, FAN53555_ID1, &val);
  383. if (ret < 0) {
  384. dev_err(&client->dev, "Failed to get chip ID!\n");
  385. return ret;
  386. }
  387. di->chip_id = val & DIE_ID;
  388. /* Get chip revision */
  389. ret = regmap_read(di->regmap, FAN53555_ID2, &val);
  390. if (ret < 0) {
  391. dev_err(&client->dev, "Failed to get chip Rev!\n");
  392. return ret;
  393. }
  394. di->chip_rev = val & DIE_REV;
  395. dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
  396. di->chip_id, di->chip_rev);
  397. /* Device init */
  398. ret = fan53555_device_setup(di, pdata);
  399. if (ret < 0) {
  400. dev_err(&client->dev, "Failed to setup device!\n");
  401. return ret;
  402. }
  403. /* Register regulator */
  404. config.dev = di->dev;
  405. config.init_data = di->regulator;
  406. config.regmap = di->regmap;
  407. config.driver_data = di;
  408. config.of_node = np;
  409. ret = fan53555_regulator_register(di, &config);
  410. if (ret < 0)
  411. dev_err(&client->dev, "Failed to register regulator!\n");
  412. return ret;
  413. }
  414. static const struct i2c_device_id fan53555_id[] = {
  415. {
  416. .name = "fan53555",
  417. .driver_data = FAN53555_VENDOR_FAIRCHILD
  418. }, {
  419. .name = "syr827",
  420. .driver_data = FAN53555_VENDOR_SILERGY
  421. }, {
  422. .name = "syr828",
  423. .driver_data = FAN53555_VENDOR_SILERGY
  424. },
  425. { },
  426. };
  427. MODULE_DEVICE_TABLE(i2c, fan53555_id);
  428. static struct i2c_driver fan53555_regulator_driver = {
  429. .driver = {
  430. .name = "fan53555-regulator",
  431. .of_match_table = of_match_ptr(fan53555_dt_ids),
  432. },
  433. .probe = fan53555_regulator_probe,
  434. .id_table = fan53555_id,
  435. };
  436. module_i2c_driver(fan53555_regulator_driver);
  437. MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
  438. MODULE_DESCRIPTION("FAN53555 regulator driver");
  439. MODULE_LICENSE("GPL v2");