as3711.c 5.6 KB

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