max8925-i2c.c 6.4 KB

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