tps65217.c 6.6 KB

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