tps65217.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * tps65217.c
  3. *
  4. * TPS65217 chip family multi-function driver
  5. *
  6. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/init.h>
  22. #include <linux/i2c.h>
  23. #include <linux/slab.h>
  24. #include <linux/regmap.h>
  25. #include <linux/err.h>
  26. #include <linux/regulator/of_regulator.h>
  27. #include <linux/mfd/core.h>
  28. #include <linux/mfd/tps65217.h>
  29. /**
  30. * tps65217_reg_read: Read a single tps65217 register.
  31. *
  32. * @tps: Device to read from.
  33. * @reg: Register to read.
  34. * @val: Contians the value
  35. */
  36. int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
  37. unsigned int *val)
  38. {
  39. return regmap_read(tps->regmap, reg, val);
  40. }
  41. EXPORT_SYMBOL_GPL(tps65217_reg_read);
  42. /**
  43. * tps65217_reg_write: Write a single tps65217 register.
  44. *
  45. * @tps65217: Device to write to.
  46. * @reg: Register to write to.
  47. * @val: Value to write.
  48. * @level: Password protected level
  49. */
  50. int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
  51. unsigned int val, unsigned int level)
  52. {
  53. int ret;
  54. unsigned int xor_reg_val;
  55. switch (level) {
  56. case TPS65217_PROTECT_NONE:
  57. return regmap_write(tps->regmap, reg, val);
  58. case TPS65217_PROTECT_L1:
  59. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  60. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  61. xor_reg_val);
  62. if (ret < 0)
  63. return ret;
  64. return regmap_write(tps->regmap, reg, val);
  65. case TPS65217_PROTECT_L2:
  66. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  67. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  68. xor_reg_val);
  69. if (ret < 0)
  70. return ret;
  71. ret = regmap_write(tps->regmap, reg, val);
  72. if (ret < 0)
  73. return ret;
  74. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  75. xor_reg_val);
  76. if (ret < 0)
  77. return ret;
  78. return regmap_write(tps->regmap, reg, val);
  79. default:
  80. return -EINVAL;
  81. }
  82. }
  83. EXPORT_SYMBOL_GPL(tps65217_reg_write);
  84. /**
  85. * tps65217_update_bits: Modify bits w.r.t mask, val and level.
  86. *
  87. * @tps65217: Device to write to.
  88. * @reg: Register to read-write to.
  89. * @mask: Mask.
  90. * @val: Value to write.
  91. * @level: Password protected level
  92. */
  93. static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
  94. unsigned int mask, unsigned int val, unsigned int level)
  95. {
  96. int ret;
  97. unsigned int data;
  98. ret = tps65217_reg_read(tps, reg, &data);
  99. if (ret) {
  100. dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
  101. return ret;
  102. }
  103. data &= ~mask;
  104. data |= val & mask;
  105. ret = tps65217_reg_write(tps, reg, data, level);
  106. if (ret)
  107. dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
  108. return ret;
  109. }
  110. int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
  111. unsigned int mask, unsigned int val, unsigned int level)
  112. {
  113. return tps65217_update_bits(tps, reg, mask, val, level);
  114. }
  115. EXPORT_SYMBOL_GPL(tps65217_set_bits);
  116. int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
  117. unsigned int mask, unsigned int level)
  118. {
  119. return tps65217_update_bits(tps, reg, mask, 0, level);
  120. }
  121. EXPORT_SYMBOL_GPL(tps65217_clear_bits);
  122. #ifdef CONFIG_OF
  123. static struct of_regulator_match reg_matches[] = {
  124. { .name = "dcdc1", .driver_data = (void *)TPS65217_DCDC_1 },
  125. { .name = "dcdc2", .driver_data = (void *)TPS65217_DCDC_2 },
  126. { .name = "dcdc3", .driver_data = (void *)TPS65217_DCDC_3 },
  127. { .name = "ldo1", .driver_data = (void *)TPS65217_LDO_1 },
  128. { .name = "ldo2", .driver_data = (void *)TPS65217_LDO_2 },
  129. { .name = "ldo3", .driver_data = (void *)TPS65217_LDO_3 },
  130. { .name = "ldo4", .driver_data = (void *)TPS65217_LDO_4 },
  131. };
  132. static struct tps65217_board *tps65217_parse_dt(struct i2c_client *client)
  133. {
  134. struct device_node *node = client->dev.of_node;
  135. struct tps65217_board *pdata;
  136. struct device_node *regs;
  137. int count = ARRAY_SIZE(reg_matches);
  138. int ret, i;
  139. regs = of_find_node_by_name(node, "regulators");
  140. if (!regs)
  141. return NULL;
  142. ret = of_regulator_match(&client->dev, regs, reg_matches, count);
  143. of_node_put(regs);
  144. if ((ret < 0) || (ret > count))
  145. return NULL;
  146. count = ret;
  147. pdata = devm_kzalloc(&client->dev, count * sizeof(*pdata), GFP_KERNEL);
  148. if (!pdata)
  149. return NULL;
  150. for (i = 0; i < count; i++) {
  151. if (!reg_matches[i].init_data || !reg_matches[i].of_node)
  152. continue;
  153. pdata->tps65217_init_data[i] = reg_matches[i].init_data;
  154. pdata->of_node[i] = reg_matches[i].of_node;
  155. }
  156. return pdata;
  157. }
  158. static struct of_device_id tps65217_of_match[] = {
  159. { .compatible = "ti,tps65217", },
  160. { },
  161. };
  162. #else
  163. static struct tps65217_board *tps65217_parse_dt(struct i2c_client *client)
  164. {
  165. return NULL;
  166. }
  167. #endif
  168. static struct regmap_config tps65217_regmap_config = {
  169. .reg_bits = 8,
  170. .val_bits = 8,
  171. };
  172. static int __devinit tps65217_probe(struct i2c_client *client,
  173. const struct i2c_device_id *ids)
  174. {
  175. struct tps65217 *tps;
  176. struct regulator_init_data *reg_data;
  177. struct tps65217_board *pdata = client->dev.platform_data;
  178. int i, ret;
  179. unsigned int version;
  180. if (!pdata && client->dev.of_node)
  181. pdata = tps65217_parse_dt(client);
  182. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  183. if (!tps)
  184. return -ENOMEM;
  185. tps->pdata = pdata;
  186. tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
  187. if (IS_ERR(tps->regmap)) {
  188. ret = PTR_ERR(tps->regmap);
  189. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  190. ret);
  191. return ret;
  192. }
  193. i2c_set_clientdata(client, tps);
  194. tps->dev = &client->dev;
  195. ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
  196. if (ret < 0) {
  197. dev_err(tps->dev, "Failed to read revision register: %d\n",
  198. ret);
  199. return ret;
  200. }
  201. dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
  202. (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
  203. version & TPS65217_CHIPID_REV_MASK);
  204. for (i = 0; i < TPS65217_NUM_REGULATOR; i++) {
  205. struct platform_device *pdev;
  206. pdev = platform_device_alloc("tps65217-pmic", i);
  207. if (!pdev) {
  208. dev_err(tps->dev, "Cannot create regulator %d\n", i);
  209. continue;
  210. }
  211. pdev->dev.parent = tps->dev;
  212. pdev->dev.of_node = pdata->of_node[i];
  213. reg_data = pdata->tps65217_init_data[i];
  214. platform_device_add_data(pdev, reg_data, sizeof(*reg_data));
  215. tps->regulator_pdev[i] = pdev;
  216. platform_device_add(pdev);
  217. }
  218. return 0;
  219. }
  220. static int __devexit tps65217_remove(struct i2c_client *client)
  221. {
  222. struct tps65217 *tps = i2c_get_clientdata(client);
  223. int i;
  224. for (i = 0; i < TPS65217_NUM_REGULATOR; i++)
  225. platform_device_unregister(tps->regulator_pdev[i]);
  226. return 0;
  227. }
  228. static const struct i2c_device_id tps65217_id_table[] = {
  229. {"tps65217", 0xF0},
  230. {/* end of list */}
  231. };
  232. MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
  233. static struct i2c_driver tps65217_driver = {
  234. .driver = {
  235. .name = "tps65217",
  236. .owner = THIS_MODULE,
  237. .of_match_table = of_match_ptr(tps65217_of_match),
  238. },
  239. .id_table = tps65217_id_table,
  240. .probe = tps65217_probe,
  241. .remove = __devexit_p(tps65217_remove),
  242. };
  243. static int __init tps65217_init(void)
  244. {
  245. return i2c_add_driver(&tps65217_driver);
  246. }
  247. subsys_initcall(tps65217_init);
  248. static void __exit tps65217_exit(void)
  249. {
  250. i2c_del_driver(&tps65217_driver);
  251. }
  252. module_exit(tps65217_exit);
  253. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  254. MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
  255. MODULE_LICENSE("GPL v2");