max8925-i2c.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * I2C driver for Maxim MAX8925
  4. *
  5. * Copyright (C) 2009 Marvell International Ltd.
  6. * Haojian Zhuang <[email protected]>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/i2c.h>
  12. #include <linux/mfd/max8925.h>
  13. #include <linux/slab.h>
  14. #define RTC_I2C_ADDR 0x68
  15. #define ADC_I2C_ADDR 0x47
  16. static inline int max8925_read_device(struct i2c_client *i2c,
  17. int reg, int bytes, void *dest)
  18. {
  19. int ret;
  20. if (bytes > 1)
  21. ret = i2c_smbus_read_i2c_block_data(i2c, reg, bytes, dest);
  22. else {
  23. ret = i2c_smbus_read_byte_data(i2c, reg);
  24. if (ret < 0)
  25. return ret;
  26. *(unsigned char *)dest = (unsigned char)ret;
  27. }
  28. return ret;
  29. }
  30. static inline int max8925_write_device(struct i2c_client *i2c,
  31. int reg, int bytes, void *src)
  32. {
  33. unsigned char buf[9];
  34. int ret;
  35. buf[0] = (unsigned char)reg;
  36. memcpy(&buf[1], src, bytes);
  37. ret = i2c_master_send(i2c, buf, bytes + 1);
  38. if (ret < 0)
  39. return ret;
  40. return 0;
  41. }
  42. int max8925_reg_read(struct i2c_client *i2c, int reg)
  43. {
  44. struct max8925_chip *chip = i2c_get_clientdata(i2c);
  45. unsigned char data = 0;
  46. int ret;
  47. mutex_lock(&chip->io_lock);
  48. ret = max8925_read_device(i2c, reg, 1, &data);
  49. mutex_unlock(&chip->io_lock);
  50. if (ret < 0)
  51. return ret;
  52. else
  53. return (int)data;
  54. }
  55. EXPORT_SYMBOL(max8925_reg_read);
  56. int max8925_reg_write(struct i2c_client *i2c, int reg,
  57. unsigned char data)
  58. {
  59. struct max8925_chip *chip = i2c_get_clientdata(i2c);
  60. int ret;
  61. mutex_lock(&chip->io_lock);
  62. ret = max8925_write_device(i2c, reg, 1, &data);
  63. mutex_unlock(&chip->io_lock);
  64. return ret;
  65. }
  66. EXPORT_SYMBOL(max8925_reg_write);
  67. int max8925_bulk_read(struct i2c_client *i2c, int reg,
  68. int count, unsigned char *buf)
  69. {
  70. struct max8925_chip *chip = i2c_get_clientdata(i2c);
  71. int ret;
  72. mutex_lock(&chip->io_lock);
  73. ret = max8925_read_device(i2c, reg, count, buf);
  74. mutex_unlock(&chip->io_lock);
  75. return ret;
  76. }
  77. EXPORT_SYMBOL(max8925_bulk_read);
  78. int max8925_bulk_write(struct i2c_client *i2c, int reg,
  79. int count, unsigned char *buf)
  80. {
  81. struct max8925_chip *chip = i2c_get_clientdata(i2c);
  82. int ret;
  83. mutex_lock(&chip->io_lock);
  84. ret = max8925_write_device(i2c, reg, count, buf);
  85. mutex_unlock(&chip->io_lock);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL(max8925_bulk_write);
  89. int max8925_set_bits(struct i2c_client *i2c, int reg,
  90. unsigned char mask, unsigned char data)
  91. {
  92. struct max8925_chip *chip = i2c_get_clientdata(i2c);
  93. unsigned char value;
  94. int ret;
  95. mutex_lock(&chip->io_lock);
  96. ret = max8925_read_device(i2c, reg, 1, &value);
  97. if (ret < 0)
  98. goto out;
  99. value &= ~mask;
  100. value |= data;
  101. ret = max8925_write_device(i2c, reg, 1, &value);
  102. out:
  103. mutex_unlock(&chip->io_lock);
  104. return ret;
  105. }
  106. EXPORT_SYMBOL(max8925_set_bits);
  107. static const struct i2c_device_id max8925_id_table[] = {
  108. { "max8925", 0 },
  109. { },
  110. };
  111. static int max8925_dt_init(struct device_node *np, struct device *dev,
  112. struct max8925_platform_data *pdata)
  113. {
  114. int ret;
  115. ret = of_property_read_u32(np, "maxim,tsc-irq", &pdata->tsc_irq);
  116. if (ret) {
  117. dev_err(dev, "Not found maxim,tsc-irq property\n");
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. static int max8925_probe(struct i2c_client *client,
  123. const struct i2c_device_id *id)
  124. {
  125. struct max8925_platform_data *pdata = dev_get_platdata(&client->dev);
  126. struct max8925_chip *chip;
  127. struct device_node *node = client->dev.of_node;
  128. if (node && !pdata) {
  129. /* parse DT to get platform data */
  130. pdata = devm_kzalloc(&client->dev,
  131. sizeof(struct max8925_platform_data),
  132. GFP_KERNEL);
  133. if (!pdata)
  134. return -ENOMEM;
  135. if (max8925_dt_init(node, &client->dev, pdata))
  136. return -EINVAL;
  137. } else if (!pdata) {
  138. pr_info("%s: platform data is missing\n", __func__);
  139. return -EINVAL;
  140. }
  141. chip = devm_kzalloc(&client->dev,
  142. sizeof(struct max8925_chip), GFP_KERNEL);
  143. if (chip == NULL)
  144. return -ENOMEM;
  145. chip->i2c = client;
  146. chip->dev = &client->dev;
  147. i2c_set_clientdata(client, chip);
  148. dev_set_drvdata(chip->dev, chip);
  149. mutex_init(&chip->io_lock);
  150. chip->rtc = i2c_new_dummy_device(chip->i2c->adapter, RTC_I2C_ADDR);
  151. if (IS_ERR(chip->rtc)) {
  152. dev_err(chip->dev, "Failed to allocate I2C device for RTC\n");
  153. return PTR_ERR(chip->rtc);
  154. }
  155. i2c_set_clientdata(chip->rtc, chip);
  156. chip->adc = i2c_new_dummy_device(chip->i2c->adapter, ADC_I2C_ADDR);
  157. if (IS_ERR(chip->adc)) {
  158. dev_err(chip->dev, "Failed to allocate I2C device for ADC\n");
  159. i2c_unregister_device(chip->rtc);
  160. return PTR_ERR(chip->adc);
  161. }
  162. i2c_set_clientdata(chip->adc, chip);
  163. device_init_wakeup(&client->dev, 1);
  164. max8925_device_init(chip, pdata);
  165. return 0;
  166. }
  167. static void max8925_remove(struct i2c_client *client)
  168. {
  169. struct max8925_chip *chip = i2c_get_clientdata(client);
  170. max8925_device_exit(chip);
  171. i2c_unregister_device(chip->adc);
  172. i2c_unregister_device(chip->rtc);
  173. }
  174. #ifdef CONFIG_PM_SLEEP
  175. static int max8925_suspend(struct device *dev)
  176. {
  177. struct i2c_client *client = to_i2c_client(dev);
  178. struct max8925_chip *chip = i2c_get_clientdata(client);
  179. if (device_may_wakeup(dev) && chip->wakeup_flag)
  180. enable_irq_wake(chip->core_irq);
  181. return 0;
  182. }
  183. static int max8925_resume(struct device *dev)
  184. {
  185. struct i2c_client *client = to_i2c_client(dev);
  186. struct max8925_chip *chip = i2c_get_clientdata(client);
  187. if (device_may_wakeup(dev) && chip->wakeup_flag)
  188. disable_irq_wake(chip->core_irq);
  189. return 0;
  190. }
  191. #endif
  192. static SIMPLE_DEV_PM_OPS(max8925_pm_ops, max8925_suspend, max8925_resume);
  193. static const struct of_device_id max8925_dt_ids[] = {
  194. { .compatible = "maxim,max8925", },
  195. {},
  196. };
  197. static struct i2c_driver max8925_driver = {
  198. .driver = {
  199. .name = "max8925",
  200. .pm = &max8925_pm_ops,
  201. .of_match_table = max8925_dt_ids,
  202. },
  203. .probe = max8925_probe,
  204. .remove = max8925_remove,
  205. .id_table = max8925_id_table,
  206. };
  207. static int __init max8925_i2c_init(void)
  208. {
  209. int ret;
  210. ret = i2c_add_driver(&max8925_driver);
  211. if (ret != 0)
  212. pr_err("Failed to register MAX8925 I2C driver: %d\n", ret);
  213. return ret;
  214. }
  215. subsys_initcall(max8925_i2c_init);