tps65217.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/mfd/core.h>
  29. #include <linux/mfd/tps65217.h>
  30. static const struct mfd_cell tps65217s[] = {
  31. {
  32. .name = "tps65217-pmic",
  33. .of_compatible = "ti,tps65217-pmic",
  34. },
  35. {
  36. .name = "tps65217-bl",
  37. .of_compatible = "ti,tps65217-bl",
  38. },
  39. {
  40. .name = "tps65217-charger",
  41. .of_compatible = "ti,tps65217-charger",
  42. },
  43. };
  44. /**
  45. * tps65217_reg_read: Read a single tps65217 register.
  46. *
  47. * @tps: Device to read from.
  48. * @reg: Register to read.
  49. * @val: Contians the value
  50. */
  51. int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
  52. unsigned int *val)
  53. {
  54. return regmap_read(tps->regmap, reg, val);
  55. }
  56. EXPORT_SYMBOL_GPL(tps65217_reg_read);
  57. /**
  58. * tps65217_reg_write: Write a single tps65217 register.
  59. *
  60. * @tps65217: Device to write to.
  61. * @reg: Register to write to.
  62. * @val: Value to write.
  63. * @level: Password protected level
  64. */
  65. int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
  66. unsigned int val, unsigned int level)
  67. {
  68. int ret;
  69. unsigned int xor_reg_val;
  70. switch (level) {
  71. case TPS65217_PROTECT_NONE:
  72. return regmap_write(tps->regmap, reg, val);
  73. case TPS65217_PROTECT_L1:
  74. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  75. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  76. xor_reg_val);
  77. if (ret < 0)
  78. return ret;
  79. return regmap_write(tps->regmap, reg, val);
  80. case TPS65217_PROTECT_L2:
  81. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  82. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  83. xor_reg_val);
  84. if (ret < 0)
  85. return ret;
  86. ret = regmap_write(tps->regmap, reg, val);
  87. if (ret < 0)
  88. return ret;
  89. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  90. xor_reg_val);
  91. if (ret < 0)
  92. return ret;
  93. return regmap_write(tps->regmap, reg, val);
  94. default:
  95. return -EINVAL;
  96. }
  97. }
  98. EXPORT_SYMBOL_GPL(tps65217_reg_write);
  99. /**
  100. * tps65217_update_bits: Modify bits w.r.t mask, val and level.
  101. *
  102. * @tps65217: Device to write to.
  103. * @reg: Register to read-write to.
  104. * @mask: Mask.
  105. * @val: Value to write.
  106. * @level: Password protected level
  107. */
  108. static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
  109. unsigned int mask, unsigned int val, unsigned int level)
  110. {
  111. int ret;
  112. unsigned int data;
  113. ret = tps65217_reg_read(tps, reg, &data);
  114. if (ret) {
  115. dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
  116. return ret;
  117. }
  118. data &= ~mask;
  119. data |= val & mask;
  120. ret = tps65217_reg_write(tps, reg, data, level);
  121. if (ret)
  122. dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
  123. return ret;
  124. }
  125. int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
  126. unsigned int mask, unsigned int val, unsigned int level)
  127. {
  128. return tps65217_update_bits(tps, reg, mask, val, level);
  129. }
  130. EXPORT_SYMBOL_GPL(tps65217_set_bits);
  131. int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
  132. unsigned int mask, unsigned int level)
  133. {
  134. return tps65217_update_bits(tps, reg, mask, 0, level);
  135. }
  136. EXPORT_SYMBOL_GPL(tps65217_clear_bits);
  137. static const struct regmap_config tps65217_regmap_config = {
  138. .reg_bits = 8,
  139. .val_bits = 8,
  140. .max_register = TPS65217_REG_MAX,
  141. };
  142. static const struct of_device_id tps65217_of_match[] = {
  143. { .compatible = "ti,tps65217", .data = (void *)TPS65217 },
  144. { /* sentinel */ },
  145. };
  146. MODULE_DEVICE_TABLE(of, tps65217_of_match);
  147. static int tps65217_probe(struct i2c_client *client,
  148. const struct i2c_device_id *ids)
  149. {
  150. struct tps65217 *tps;
  151. unsigned int version;
  152. unsigned long chip_id = ids->driver_data;
  153. const struct of_device_id *match;
  154. bool status_off = false;
  155. int ret;
  156. if (client->dev.of_node) {
  157. match = of_match_device(tps65217_of_match, &client->dev);
  158. if (!match) {
  159. dev_err(&client->dev,
  160. "Failed to find matching dt id\n");
  161. return -EINVAL;
  162. }
  163. chip_id = (unsigned long)match->data;
  164. status_off = of_property_read_bool(client->dev.of_node,
  165. "ti,pmic-shutdown-controller");
  166. }
  167. if (!chip_id) {
  168. dev_err(&client->dev, "id is null.\n");
  169. return -ENODEV;
  170. }
  171. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  172. if (!tps)
  173. return -ENOMEM;
  174. i2c_set_clientdata(client, tps);
  175. tps->dev = &client->dev;
  176. tps->id = chip_id;
  177. tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
  178. if (IS_ERR(tps->regmap)) {
  179. ret = PTR_ERR(tps->regmap);
  180. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  181. ret);
  182. return ret;
  183. }
  184. ret = mfd_add_devices(tps->dev, -1, tps65217s,
  185. ARRAY_SIZE(tps65217s), NULL, 0, NULL);
  186. if (ret < 0) {
  187. dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
  188. return ret;
  189. }
  190. ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
  191. if (ret < 0) {
  192. dev_err(tps->dev, "Failed to read revision register: %d\n",
  193. ret);
  194. return ret;
  195. }
  196. /* Set the PMIC to shutdown on PWR_EN toggle */
  197. if (status_off) {
  198. ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
  199. TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
  200. TPS65217_PROTECT_NONE);
  201. if (ret)
  202. dev_warn(tps->dev, "unable to set the status OFF\n");
  203. }
  204. dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
  205. (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
  206. version & TPS65217_CHIPID_REV_MASK);
  207. return 0;
  208. }
  209. static int tps65217_remove(struct i2c_client *client)
  210. {
  211. struct tps65217 *tps = i2c_get_clientdata(client);
  212. mfd_remove_devices(tps->dev);
  213. return 0;
  214. }
  215. static const struct i2c_device_id tps65217_id_table[] = {
  216. {"tps65217", TPS65217},
  217. { /* sentinel */ }
  218. };
  219. MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
  220. static struct i2c_driver tps65217_driver = {
  221. .driver = {
  222. .name = "tps65217",
  223. .of_match_table = tps65217_of_match,
  224. },
  225. .id_table = tps65217_id_table,
  226. .probe = tps65217_probe,
  227. .remove = tps65217_remove,
  228. };
  229. static int __init tps65217_init(void)
  230. {
  231. return i2c_add_driver(&tps65217_driver);
  232. }
  233. subsys_initcall(tps65217_init);
  234. static void __exit tps65217_exit(void)
  235. {
  236. i2c_del_driver(&tps65217_driver);
  237. }
  238. module_exit(tps65217_exit);
  239. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  240. MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
  241. MODULE_LICENSE("GPL v2");