as3711.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * AS3711 PMIC MFC driver
  3. *
  4. * Copyright (C) 2012 Renesas Electronics Corporation
  5. * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation
  10. */
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mfd/as3711.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. enum {
  23. AS3711_REGULATOR,
  24. AS3711_BACKLIGHT,
  25. };
  26. /*
  27. * Ok to have it static: it is only used during probing and multiple I2C devices
  28. * cannot be probed simultaneously. Just make sure to avoid stale data.
  29. */
  30. static struct mfd_cell as3711_subdevs[] = {
  31. [AS3711_REGULATOR] = {.name = "as3711-regulator",},
  32. [AS3711_BACKLIGHT] = {.name = "as3711-backlight",},
  33. };
  34. static bool as3711_volatile_reg(struct device *dev, unsigned int reg)
  35. {
  36. switch (reg) {
  37. case AS3711_GPIO_SIGNAL_IN:
  38. case AS3711_INTERRUPT_STATUS_1:
  39. case AS3711_INTERRUPT_STATUS_2:
  40. case AS3711_INTERRUPT_STATUS_3:
  41. case AS3711_CHARGER_STATUS_1:
  42. case AS3711_CHARGER_STATUS_2:
  43. case AS3711_REG_STATUS:
  44. return true;
  45. }
  46. return false;
  47. }
  48. static bool as3711_precious_reg(struct device *dev, unsigned int reg)
  49. {
  50. switch (reg) {
  51. case AS3711_INTERRUPT_STATUS_1:
  52. case AS3711_INTERRUPT_STATUS_2:
  53. case AS3711_INTERRUPT_STATUS_3:
  54. return true;
  55. }
  56. return false;
  57. }
  58. static bool as3711_readable_reg(struct device *dev, unsigned int reg)
  59. {
  60. switch (reg) {
  61. case AS3711_SD_1_VOLTAGE:
  62. case AS3711_SD_2_VOLTAGE:
  63. case AS3711_SD_3_VOLTAGE:
  64. case AS3711_SD_4_VOLTAGE:
  65. case AS3711_LDO_1_VOLTAGE:
  66. case AS3711_LDO_2_VOLTAGE:
  67. case AS3711_LDO_3_VOLTAGE:
  68. case AS3711_LDO_4_VOLTAGE:
  69. case AS3711_LDO_5_VOLTAGE:
  70. case AS3711_LDO_6_VOLTAGE:
  71. case AS3711_LDO_7_VOLTAGE:
  72. case AS3711_LDO_8_VOLTAGE:
  73. case AS3711_SD_CONTROL:
  74. case AS3711_GPIO_SIGNAL_OUT:
  75. case AS3711_GPIO_SIGNAL_IN:
  76. case AS3711_SD_CONTROL_1:
  77. case AS3711_SD_CONTROL_2:
  78. case AS3711_CURR_CONTROL:
  79. case AS3711_CURR1_VALUE:
  80. case AS3711_CURR2_VALUE:
  81. case AS3711_CURR3_VALUE:
  82. case AS3711_STEPUP_CONTROL_1:
  83. case AS3711_STEPUP_CONTROL_2:
  84. case AS3711_STEPUP_CONTROL_4:
  85. case AS3711_STEPUP_CONTROL_5:
  86. case AS3711_REG_STATUS:
  87. case AS3711_INTERRUPT_STATUS_1:
  88. case AS3711_INTERRUPT_STATUS_2:
  89. case AS3711_INTERRUPT_STATUS_3:
  90. case AS3711_CHARGER_STATUS_1:
  91. case AS3711_CHARGER_STATUS_2:
  92. case AS3711_ASIC_ID_1:
  93. case AS3711_ASIC_ID_2:
  94. return true;
  95. }
  96. return false;
  97. }
  98. static const struct regmap_config as3711_regmap_config = {
  99. .reg_bits = 8,
  100. .val_bits = 8,
  101. .volatile_reg = as3711_volatile_reg,
  102. .readable_reg = as3711_readable_reg,
  103. .precious_reg = as3711_precious_reg,
  104. .max_register = AS3711_MAX_REGS,
  105. .num_reg_defaults_raw = AS3711_MAX_REGS,
  106. .cache_type = REGCACHE_RBTREE,
  107. };
  108. #ifdef CONFIG_OF
  109. static const struct of_device_id as3711_of_match[] = {
  110. {.compatible = "ams,as3711",},
  111. {}
  112. };
  113. MODULE_DEVICE_TABLE(of, as3711_of_match);
  114. #endif
  115. static int as3711_i2c_probe(struct i2c_client *client,
  116. const struct i2c_device_id *id)
  117. {
  118. struct as3711 *as3711;
  119. struct as3711_platform_data *pdata;
  120. unsigned int id1, id2;
  121. int ret;
  122. if (!client->dev.of_node) {
  123. pdata = dev_get_platdata(&client->dev);
  124. if (!pdata)
  125. dev_dbg(&client->dev, "Platform data not found\n");
  126. } else {
  127. pdata = devm_kzalloc(&client->dev,
  128. sizeof(*pdata), GFP_KERNEL);
  129. if (!pdata) {
  130. dev_err(&client->dev, "Failed to allocate pdata\n");
  131. return -ENOMEM;
  132. }
  133. }
  134. as3711 = devm_kzalloc(&client->dev, sizeof(struct as3711), GFP_KERNEL);
  135. if (!as3711) {
  136. dev_err(&client->dev, "Memory allocation failed\n");
  137. return -ENOMEM;
  138. }
  139. as3711->dev = &client->dev;
  140. i2c_set_clientdata(client, as3711);
  141. if (client->irq)
  142. dev_notice(&client->dev, "IRQ not supported yet\n");
  143. as3711->regmap = devm_regmap_init_i2c(client, &as3711_regmap_config);
  144. if (IS_ERR(as3711->regmap)) {
  145. ret = PTR_ERR(as3711->regmap);
  146. dev_err(&client->dev, "regmap initialization failed: %d\n", ret);
  147. return ret;
  148. }
  149. ret = regmap_read(as3711->regmap, AS3711_ASIC_ID_1, &id1);
  150. if (!ret)
  151. ret = regmap_read(as3711->regmap, AS3711_ASIC_ID_2, &id2);
  152. if (ret < 0) {
  153. dev_err(&client->dev, "regmap_read() failed: %d\n", ret);
  154. return ret;
  155. }
  156. if (id1 != 0x8b)
  157. return -ENODEV;
  158. dev_info(as3711->dev, "AS3711 detected: %x:%x\n", id1, id2);
  159. /* We can reuse as3711_subdevs[], it will be copied in mfd_add_devices() */
  160. if (pdata) {
  161. as3711_subdevs[AS3711_REGULATOR].platform_data = &pdata->regulator;
  162. as3711_subdevs[AS3711_REGULATOR].pdata_size = sizeof(pdata->regulator);
  163. as3711_subdevs[AS3711_BACKLIGHT].platform_data = &pdata->backlight;
  164. as3711_subdevs[AS3711_BACKLIGHT].pdata_size = sizeof(pdata->backlight);
  165. } else {
  166. as3711_subdevs[AS3711_REGULATOR].platform_data = NULL;
  167. as3711_subdevs[AS3711_REGULATOR].pdata_size = 0;
  168. as3711_subdevs[AS3711_BACKLIGHT].platform_data = NULL;
  169. as3711_subdevs[AS3711_BACKLIGHT].pdata_size = 0;
  170. }
  171. ret = mfd_add_devices(as3711->dev, -1, as3711_subdevs,
  172. ARRAY_SIZE(as3711_subdevs), NULL, 0, NULL);
  173. if (ret < 0)
  174. dev_err(&client->dev, "add mfd devices failed: %d\n", ret);
  175. return ret;
  176. }
  177. static int as3711_i2c_remove(struct i2c_client *client)
  178. {
  179. struct as3711 *as3711 = i2c_get_clientdata(client);
  180. mfd_remove_devices(as3711->dev);
  181. return 0;
  182. }
  183. static const struct i2c_device_id as3711_i2c_id[] = {
  184. {.name = "as3711", .driver_data = 0},
  185. {}
  186. };
  187. MODULE_DEVICE_TABLE(i2c, as3711_i2c_id);
  188. static struct i2c_driver as3711_i2c_driver = {
  189. .driver = {
  190. .name = "as3711",
  191. .of_match_table = of_match_ptr(as3711_of_match),
  192. },
  193. .probe = as3711_i2c_probe,
  194. .remove = as3711_i2c_remove,
  195. .id_table = as3711_i2c_id,
  196. };
  197. static int __init as3711_i2c_init(void)
  198. {
  199. return i2c_add_driver(&as3711_i2c_driver);
  200. }
  201. /* Initialise early */
  202. subsys_initcall(as3711_i2c_init);
  203. static void __exit as3711_i2c_exit(void)
  204. {
  205. i2c_del_driver(&as3711_i2c_driver);
  206. }
  207. module_exit(as3711_i2c_exit);
  208. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  209. MODULE_DESCRIPTION("AS3711 PMIC driver");
  210. MODULE_LICENSE("GPL v2");