tps65217.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. static int tps65217_probe(struct i2c_client *client,
  143. const struct i2c_device_id *ids)
  144. {
  145. struct tps65217 *tps;
  146. unsigned int version;
  147. unsigned long chip_id = ids->driver_data;
  148. const struct of_device_id *match;
  149. bool status_off = false;
  150. int ret;
  151. if (client->dev.of_node) {
  152. match = of_match_device(tps65217_of_match, &client->dev);
  153. if (!match) {
  154. dev_err(&client->dev,
  155. "Failed to find matching dt id\n");
  156. return -EINVAL;
  157. }
  158. chip_id = (unsigned long)match->data;
  159. status_off = of_property_read_bool(client->dev.of_node,
  160. "ti,pmic-shutdown-controller");
  161. }
  162. if (!chip_id) {
  163. dev_err(&client->dev, "id is null.\n");
  164. return -ENODEV;
  165. }
  166. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  167. if (!tps)
  168. return -ENOMEM;
  169. i2c_set_clientdata(client, tps);
  170. tps->dev = &client->dev;
  171. tps->id = chip_id;
  172. tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
  173. if (IS_ERR(tps->regmap)) {
  174. ret = PTR_ERR(tps->regmap);
  175. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  176. ret);
  177. return ret;
  178. }
  179. ret = mfd_add_devices(tps->dev, -1, tps65217s,
  180. ARRAY_SIZE(tps65217s), NULL, 0, NULL);
  181. if (ret < 0) {
  182. dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
  183. return ret;
  184. }
  185. ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
  186. if (ret < 0) {
  187. dev_err(tps->dev, "Failed to read revision register: %d\n",
  188. ret);
  189. return ret;
  190. }
  191. /* Set the PMIC to shutdown on PWR_EN toggle */
  192. if (status_off) {
  193. ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
  194. TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
  195. TPS65217_PROTECT_NONE);
  196. if (ret)
  197. dev_warn(tps->dev, "unable to set the status OFF\n");
  198. }
  199. dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
  200. (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
  201. version & TPS65217_CHIPID_REV_MASK);
  202. return 0;
  203. }
  204. static int tps65217_remove(struct i2c_client *client)
  205. {
  206. struct tps65217 *tps = i2c_get_clientdata(client);
  207. mfd_remove_devices(tps->dev);
  208. return 0;
  209. }
  210. static const struct i2c_device_id tps65217_id_table[] = {
  211. {"tps65217", TPS65217},
  212. { /* sentinel */ }
  213. };
  214. MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
  215. static struct i2c_driver tps65217_driver = {
  216. .driver = {
  217. .name = "tps65217",
  218. .owner = THIS_MODULE,
  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");