da9063-i2c.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* da9063-i2c.c: Interrupt support for Dialog DA9063
  2. *
  3. * Copyright 2012 Dialog Semiconductor Ltd.
  4. * Copyright 2013 Philipp Zabel, Pengutronix
  5. *
  6. * Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/mfd/da9063/core.h>
  23. #include <linux/mfd/da9063/pdata.h>
  24. #include <linux/mfd/da9063/registers.h>
  25. static const struct regmap_range da9063_readable_ranges[] = {
  26. {
  27. .range_min = DA9063_REG_PAGE_CON,
  28. .range_max = DA9063_REG_SECOND_D,
  29. }, {
  30. .range_min = DA9063_REG_SEQ,
  31. .range_max = DA9063_REG_ID_32_31,
  32. }, {
  33. .range_min = DA9063_REG_SEQ_A,
  34. .range_max = DA9063_REG_AUTO3_LOW,
  35. }, {
  36. .range_min = DA9063_REG_T_OFFSET,
  37. .range_max = DA9063_REG_GP_ID_19,
  38. }, {
  39. .range_min = DA9063_REG_CHIP_ID,
  40. .range_max = DA9063_REG_CHIP_VARIANT,
  41. },
  42. };
  43. static const struct regmap_range da9063_writeable_ranges[] = {
  44. {
  45. .range_min = DA9063_REG_PAGE_CON,
  46. .range_max = DA9063_REG_PAGE_CON,
  47. }, {
  48. .range_min = DA9063_REG_FAULT_LOG,
  49. .range_max = DA9063_REG_VSYS_MON,
  50. }, {
  51. .range_min = DA9063_REG_COUNT_S,
  52. .range_max = DA9063_REG_ALARM_Y,
  53. }, {
  54. .range_min = DA9063_REG_SEQ,
  55. .range_max = DA9063_REG_ID_32_31,
  56. }, {
  57. .range_min = DA9063_REG_SEQ_A,
  58. .range_max = DA9063_REG_AUTO3_LOW,
  59. }, {
  60. .range_min = DA9063_REG_CONFIG_I,
  61. .range_max = DA9063_REG_MON_REG_4,
  62. }, {
  63. .range_min = DA9063_REG_GP_ID_0,
  64. .range_max = DA9063_REG_GP_ID_19,
  65. },
  66. };
  67. static const struct regmap_range da9063_volatile_ranges[] = {
  68. {
  69. .range_min = DA9063_REG_STATUS_A,
  70. .range_max = DA9063_REG_EVENT_D,
  71. }, {
  72. .range_min = DA9063_REG_CONTROL_F,
  73. .range_max = DA9063_REG_CONTROL_F,
  74. }, {
  75. .range_min = DA9063_REG_ADC_MAN,
  76. .range_max = DA9063_REG_ADC_MAN,
  77. }, {
  78. .range_min = DA9063_REG_ADC_RES_L,
  79. .range_max = DA9063_REG_SECOND_D,
  80. }, {
  81. .range_min = DA9063_REG_MON_REG_5,
  82. .range_max = DA9063_REG_MON_REG_6,
  83. },
  84. };
  85. static const struct regmap_access_table da9063_readable_table = {
  86. .yes_ranges = da9063_readable_ranges,
  87. .n_yes_ranges = ARRAY_SIZE(da9063_readable_ranges),
  88. };
  89. static const struct regmap_access_table da9063_writeable_table = {
  90. .yes_ranges = da9063_writeable_ranges,
  91. .n_yes_ranges = ARRAY_SIZE(da9063_writeable_ranges),
  92. };
  93. static const struct regmap_access_table da9063_volatile_table = {
  94. .yes_ranges = da9063_volatile_ranges,
  95. .n_yes_ranges = ARRAY_SIZE(da9063_volatile_ranges),
  96. };
  97. static const struct regmap_range_cfg da9063_range_cfg[] = {
  98. {
  99. .range_min = DA9063_REG_PAGE_CON,
  100. .range_max = DA9063_REG_CHIP_VARIANT,
  101. .selector_reg = DA9063_REG_PAGE_CON,
  102. .selector_mask = 1 << DA9063_I2C_PAGE_SEL_SHIFT,
  103. .selector_shift = DA9063_I2C_PAGE_SEL_SHIFT,
  104. .window_start = 0,
  105. .window_len = 256,
  106. }
  107. };
  108. static struct regmap_config da9063_regmap_config = {
  109. .reg_bits = 8,
  110. .val_bits = 8,
  111. .ranges = da9063_range_cfg,
  112. .num_ranges = ARRAY_SIZE(da9063_range_cfg),
  113. .max_register = DA9063_REG_CHIP_VARIANT,
  114. .cache_type = REGCACHE_RBTREE,
  115. .rd_table = &da9063_readable_table,
  116. .wr_table = &da9063_writeable_table,
  117. .volatile_table = &da9063_volatile_table,
  118. };
  119. static int da9063_i2c_probe(struct i2c_client *i2c,
  120. const struct i2c_device_id *id)
  121. {
  122. struct da9063 *da9063;
  123. int ret;
  124. da9063 = devm_kzalloc(&i2c->dev, sizeof(struct da9063), GFP_KERNEL);
  125. if (da9063 == NULL)
  126. return -ENOMEM;
  127. i2c_set_clientdata(i2c, da9063);
  128. da9063->dev = &i2c->dev;
  129. da9063->chip_irq = i2c->irq;
  130. da9063->regmap = devm_regmap_init_i2c(i2c, &da9063_regmap_config);
  131. if (IS_ERR(da9063->regmap)) {
  132. ret = PTR_ERR(da9063->regmap);
  133. dev_err(da9063->dev, "Failed to allocate register map: %d\n",
  134. ret);
  135. return ret;
  136. }
  137. return da9063_device_init(da9063, i2c->irq);
  138. }
  139. static int da9063_i2c_remove(struct i2c_client *i2c)
  140. {
  141. struct da9063 *da9063 = i2c_get_clientdata(i2c);
  142. da9063_device_exit(da9063);
  143. return 0;
  144. }
  145. static const struct i2c_device_id da9063_i2c_id[] = {
  146. {"da9063", PMIC_DA9063},
  147. {},
  148. };
  149. MODULE_DEVICE_TABLE(i2c, da9063_i2c_id);
  150. static struct i2c_driver da9063_i2c_driver = {
  151. .driver = {
  152. .name = "da9063",
  153. .owner = THIS_MODULE,
  154. },
  155. .probe = da9063_i2c_probe,
  156. .remove = da9063_i2c_remove,
  157. .id_table = da9063_i2c_id,
  158. };
  159. module_i2c_driver(da9063_i2c_driver);