max8925-i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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[bytes + 1];
  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 __devinit max8925_probe(struct i2c_client *client,
  116. const struct i2c_device_id *id)
  117. {
  118. struct max8925_platform_data *pdata = client->dev.platform_data;
  119. static struct max8925_chip *chip;
  120. if (!pdata) {
  121. pr_info("%s: platform data is missing\n", __func__);
  122. return -EINVAL;
  123. }
  124. chip = kzalloc(sizeof(struct max8925_chip), GFP_KERNEL);
  125. if (chip == NULL)
  126. return -ENOMEM;
  127. chip->i2c = client;
  128. chip->dev = &client->dev;
  129. i2c_set_clientdata(client, chip);
  130. dev_set_drvdata(chip->dev, chip);
  131. mutex_init(&chip->io_lock);
  132. chip->rtc = i2c_new_dummy(chip->i2c->adapter, RTC_I2C_ADDR);
  133. i2c_set_clientdata(chip->rtc, chip);
  134. chip->adc = i2c_new_dummy(chip->i2c->adapter, ADC_I2C_ADDR);
  135. i2c_set_clientdata(chip->adc, chip);
  136. max8925_device_init(chip, pdata);
  137. return 0;
  138. }
  139. static int __devexit max8925_remove(struct i2c_client *client)
  140. {
  141. struct max8925_chip *chip = i2c_get_clientdata(client);
  142. max8925_device_exit(chip);
  143. i2c_unregister_device(chip->adc);
  144. i2c_unregister_device(chip->rtc);
  145. kfree(chip);
  146. return 0;
  147. }
  148. static struct i2c_driver max8925_driver = {
  149. .driver = {
  150. .name = "max8925",
  151. .owner = THIS_MODULE,
  152. },
  153. .probe = max8925_probe,
  154. .remove = __devexit_p(max8925_remove),
  155. .id_table = max8925_id_table,
  156. };
  157. static int __init max8925_i2c_init(void)
  158. {
  159. int ret;
  160. ret = i2c_add_driver(&max8925_driver);
  161. if (ret != 0)
  162. pr_err("Failed to register MAX8925 I2C driver: %d\n", ret);
  163. return ret;
  164. }
  165. subsys_initcall(max8925_i2c_init);
  166. static void __exit max8925_i2c_exit(void)
  167. {
  168. i2c_del_driver(&max8925_driver);
  169. }
  170. module_exit(max8925_i2c_exit);
  171. MODULE_DESCRIPTION("I2C Driver for Maxim 8925");
  172. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  173. MODULE_LICENSE("GPL");