tps65217.c 6.1 KB

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