as3711.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_REG,
  105. .num_reg_defaults_raw = AS3711_NUM_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. return -ENOMEM;
  131. }
  132. as3711 = devm_kzalloc(&client->dev, sizeof(struct as3711), GFP_KERNEL);
  133. if (!as3711)
  134. return -ENOMEM;
  135. as3711->dev = &client->dev;
  136. i2c_set_clientdata(client, as3711);
  137. if (client->irq)
  138. dev_notice(&client->dev, "IRQ not supported yet\n");
  139. as3711->regmap = devm_regmap_init_i2c(client, &as3711_regmap_config);
  140. if (IS_ERR(as3711->regmap)) {
  141. ret = PTR_ERR(as3711->regmap);
  142. dev_err(&client->dev,
  143. "regmap initialization failed: %d\n", ret);
  144. return ret;
  145. }
  146. ret = regmap_read(as3711->regmap, AS3711_ASIC_ID_1, &id1);
  147. if (!ret)
  148. ret = regmap_read(as3711->regmap, AS3711_ASIC_ID_2, &id2);
  149. if (ret < 0) {
  150. dev_err(&client->dev, "regmap_read() failed: %d\n", ret);
  151. return ret;
  152. }
  153. if (id1 != 0x8b)
  154. return -ENODEV;
  155. dev_info(as3711->dev, "AS3711 detected: %x:%x\n", id1, id2);
  156. /*
  157. * We can reuse as3711_subdevs[],
  158. * it will be copied in mfd_add_devices()
  159. */
  160. if (pdata) {
  161. as3711_subdevs[AS3711_REGULATOR].platform_data =
  162. &pdata->regulator;
  163. as3711_subdevs[AS3711_REGULATOR].pdata_size =
  164. sizeof(pdata->regulator);
  165. as3711_subdevs[AS3711_BACKLIGHT].platform_data =
  166. &pdata->backlight;
  167. as3711_subdevs[AS3711_BACKLIGHT].pdata_size =
  168. sizeof(pdata->backlight);
  169. } else {
  170. as3711_subdevs[AS3711_REGULATOR].platform_data = NULL;
  171. as3711_subdevs[AS3711_REGULATOR].pdata_size = 0;
  172. as3711_subdevs[AS3711_BACKLIGHT].platform_data = NULL;
  173. as3711_subdevs[AS3711_BACKLIGHT].pdata_size = 0;
  174. }
  175. ret = devm_mfd_add_devices(as3711->dev, -1, as3711_subdevs,
  176. ARRAY_SIZE(as3711_subdevs), NULL, 0, NULL);
  177. if (ret < 0)
  178. dev_err(&client->dev, "add mfd devices failed: %d\n", ret);
  179. return ret;
  180. }
  181. static const struct i2c_device_id as3711_i2c_id[] = {
  182. {.name = "as3711", .driver_data = 0},
  183. {}
  184. };
  185. MODULE_DEVICE_TABLE(i2c, as3711_i2c_id);
  186. static struct i2c_driver as3711_i2c_driver = {
  187. .driver = {
  188. .name = "as3711",
  189. .of_match_table = of_match_ptr(as3711_of_match),
  190. },
  191. .probe = as3711_i2c_probe,
  192. .id_table = as3711_i2c_id,
  193. };
  194. static int __init as3711_i2c_init(void)
  195. {
  196. return i2c_add_driver(&as3711_i2c_driver);
  197. }
  198. /* Initialise early */
  199. subsys_initcall(as3711_i2c_init);
  200. static void __exit as3711_i2c_exit(void)
  201. {
  202. i2c_del_driver(&as3711_i2c_driver);
  203. }
  204. module_exit(as3711_i2c_exit);
  205. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  206. MODULE_DESCRIPTION("AS3711 PMIC driver");
  207. MODULE_LICENSE("GPL v2");