88pm860x-i2c.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * I2C driver for Marvell 88PM860x
  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/88pm860x.h>
  16. #include <linux/slab.h>
  17. static inline int pm860x_read_device(struct i2c_client *i2c,
  18. int reg, int bytes, void *dest)
  19. {
  20. unsigned char data;
  21. int ret;
  22. data = (unsigned char)reg;
  23. ret = i2c_master_send(i2c, &data, 1);
  24. if (ret < 0)
  25. return ret;
  26. ret = i2c_master_recv(i2c, dest, bytes);
  27. if (ret < 0)
  28. return ret;
  29. return 0;
  30. }
  31. static inline int pm860x_write_device(struct i2c_client *i2c,
  32. int reg, int bytes, void *src)
  33. {
  34. unsigned char buf[bytes + 1];
  35. int ret;
  36. buf[0] = (unsigned char)reg;
  37. memcpy(&buf[1], src, bytes);
  38. ret = i2c_master_send(i2c, buf, bytes + 1);
  39. if (ret < 0)
  40. return ret;
  41. return 0;
  42. }
  43. int pm860x_reg_read(struct i2c_client *i2c, int reg)
  44. {
  45. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  46. unsigned char data;
  47. int ret;
  48. mutex_lock(&chip->io_lock);
  49. ret = pm860x_read_device(i2c, reg, 1, &data);
  50. mutex_unlock(&chip->io_lock);
  51. if (ret < 0)
  52. return ret;
  53. else
  54. return (int)data;
  55. }
  56. EXPORT_SYMBOL(pm860x_reg_read);
  57. int pm860x_reg_write(struct i2c_client *i2c, int reg,
  58. unsigned char data)
  59. {
  60. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  61. int ret;
  62. mutex_lock(&chip->io_lock);
  63. ret = pm860x_write_device(i2c, reg, 1, &data);
  64. mutex_unlock(&chip->io_lock);
  65. return ret;
  66. }
  67. EXPORT_SYMBOL(pm860x_reg_write);
  68. int pm860x_bulk_read(struct i2c_client *i2c, int reg,
  69. int count, unsigned char *buf)
  70. {
  71. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  72. int ret;
  73. mutex_lock(&chip->io_lock);
  74. ret = pm860x_read_device(i2c, reg, count, buf);
  75. mutex_unlock(&chip->io_lock);
  76. return ret;
  77. }
  78. EXPORT_SYMBOL(pm860x_bulk_read);
  79. int pm860x_bulk_write(struct i2c_client *i2c, int reg,
  80. int count, unsigned char *buf)
  81. {
  82. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  83. int ret;
  84. mutex_lock(&chip->io_lock);
  85. ret = pm860x_write_device(i2c, reg, count, buf);
  86. mutex_unlock(&chip->io_lock);
  87. return ret;
  88. }
  89. EXPORT_SYMBOL(pm860x_bulk_write);
  90. int pm860x_set_bits(struct i2c_client *i2c, int reg,
  91. unsigned char mask, unsigned char data)
  92. {
  93. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  94. unsigned char value;
  95. int ret;
  96. mutex_lock(&chip->io_lock);
  97. ret = pm860x_read_device(i2c, reg, 1, &value);
  98. if (ret < 0)
  99. goto out;
  100. value &= ~mask;
  101. value |= data;
  102. ret = pm860x_write_device(i2c, reg, 1, &value);
  103. out:
  104. mutex_unlock(&chip->io_lock);
  105. return ret;
  106. }
  107. EXPORT_SYMBOL(pm860x_set_bits);
  108. int pm860x_page_reg_read(struct i2c_client *i2c, int reg)
  109. {
  110. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  111. unsigned char zero = 0;
  112. unsigned char data;
  113. int ret;
  114. mutex_lock(&chip->io_lock);
  115. pm860x_write_device(i2c, 0xFA, 0, &zero);
  116. pm860x_write_device(i2c, 0xFB, 0, &zero);
  117. pm860x_write_device(i2c, 0xFF, 0, &zero);
  118. ret = pm860x_read_device(i2c, reg, 1, &data);
  119. if (ret >= 0)
  120. ret = (int)data;
  121. pm860x_write_device(i2c, 0xFE, 0, &zero);
  122. pm860x_write_device(i2c, 0xFC, 0, &zero);
  123. mutex_unlock(&chip->io_lock);
  124. return ret;
  125. }
  126. EXPORT_SYMBOL(pm860x_page_reg_read);
  127. int pm860x_page_reg_write(struct i2c_client *i2c, int reg,
  128. unsigned char data)
  129. {
  130. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  131. unsigned char zero;
  132. int ret;
  133. mutex_lock(&chip->io_lock);
  134. pm860x_write_device(i2c, 0xFA, 0, &zero);
  135. pm860x_write_device(i2c, 0xFB, 0, &zero);
  136. pm860x_write_device(i2c, 0xFF, 0, &zero);
  137. ret = pm860x_write_device(i2c, reg, 1, &data);
  138. pm860x_write_device(i2c, 0xFE, 0, &zero);
  139. pm860x_write_device(i2c, 0xFC, 0, &zero);
  140. mutex_unlock(&chip->io_lock);
  141. return ret;
  142. }
  143. EXPORT_SYMBOL(pm860x_page_reg_write);
  144. int pm860x_page_bulk_read(struct i2c_client *i2c, int reg,
  145. int count, unsigned char *buf)
  146. {
  147. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  148. unsigned char zero = 0;
  149. int ret;
  150. mutex_lock(&chip->io_lock);
  151. pm860x_write_device(i2c, 0xFA, 0, &zero);
  152. pm860x_write_device(i2c, 0xFB, 0, &zero);
  153. pm860x_write_device(i2c, 0xFF, 0, &zero);
  154. ret = pm860x_read_device(i2c, reg, count, buf);
  155. pm860x_write_device(i2c, 0xFE, 0, &zero);
  156. pm860x_write_device(i2c, 0xFC, 0, &zero);
  157. mutex_unlock(&chip->io_lock);
  158. return ret;
  159. }
  160. EXPORT_SYMBOL(pm860x_page_bulk_read);
  161. int pm860x_page_bulk_write(struct i2c_client *i2c, int reg,
  162. int count, unsigned char *buf)
  163. {
  164. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  165. unsigned char zero = 0;
  166. int ret;
  167. mutex_lock(&chip->io_lock);
  168. pm860x_write_device(i2c, 0xFA, 0, &zero);
  169. pm860x_write_device(i2c, 0xFB, 0, &zero);
  170. pm860x_write_device(i2c, 0xFF, 0, &zero);
  171. ret = pm860x_write_device(i2c, reg, count, buf);
  172. pm860x_write_device(i2c, 0xFE, 0, &zero);
  173. pm860x_write_device(i2c, 0xFC, 0, &zero);
  174. mutex_unlock(&chip->io_lock);
  175. return ret;
  176. }
  177. EXPORT_SYMBOL(pm860x_page_bulk_write);
  178. int pm860x_page_set_bits(struct i2c_client *i2c, int reg,
  179. unsigned char mask, unsigned char data)
  180. {
  181. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  182. unsigned char zero;
  183. unsigned char value;
  184. int ret;
  185. mutex_lock(&chip->io_lock);
  186. pm860x_write_device(i2c, 0xFA, 0, &zero);
  187. pm860x_write_device(i2c, 0xFB, 0, &zero);
  188. pm860x_write_device(i2c, 0xFF, 0, &zero);
  189. ret = pm860x_read_device(i2c, reg, 1, &value);
  190. if (ret < 0)
  191. goto out;
  192. value &= ~mask;
  193. value |= data;
  194. ret = pm860x_write_device(i2c, reg, 1, &value);
  195. out:
  196. pm860x_write_device(i2c, 0xFE, 0, &zero);
  197. pm860x_write_device(i2c, 0xFC, 0, &zero);
  198. mutex_unlock(&chip->io_lock);
  199. return ret;
  200. }
  201. EXPORT_SYMBOL(pm860x_page_set_bits);
  202. static const struct i2c_device_id pm860x_id_table[] = {
  203. { "88PM860x", 0 },
  204. {}
  205. };
  206. MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
  207. static int verify_addr(struct i2c_client *i2c)
  208. {
  209. unsigned short addr_8607[] = {0x30, 0x34};
  210. unsigned short addr_8606[] = {0x10, 0x11};
  211. int size, i;
  212. if (i2c == NULL)
  213. return 0;
  214. size = ARRAY_SIZE(addr_8606);
  215. for (i = 0; i < size; i++) {
  216. if (i2c->addr == *(addr_8606 + i))
  217. return CHIP_PM8606;
  218. }
  219. size = ARRAY_SIZE(addr_8607);
  220. for (i = 0; i < size; i++) {
  221. if (i2c->addr == *(addr_8607 + i))
  222. return CHIP_PM8607;
  223. }
  224. return 0;
  225. }
  226. static int __devinit pm860x_probe(struct i2c_client *client,
  227. const struct i2c_device_id *id)
  228. {
  229. struct pm860x_platform_data *pdata = client->dev.platform_data;
  230. struct pm860x_chip *chip;
  231. if (!pdata) {
  232. pr_info("No platform data in %s!\n", __func__);
  233. return -EINVAL;
  234. }
  235. chip = kzalloc(sizeof(struct pm860x_chip), GFP_KERNEL);
  236. if (chip == NULL)
  237. return -ENOMEM;
  238. chip->id = verify_addr(client);
  239. chip->client = client;
  240. i2c_set_clientdata(client, chip);
  241. chip->dev = &client->dev;
  242. mutex_init(&chip->io_lock);
  243. dev_set_drvdata(chip->dev, chip);
  244. /*
  245. * Both client and companion client shares same platform driver.
  246. * Driver distinguishes them by pdata->companion_addr.
  247. * pdata->companion_addr is only assigned if companion chip exists.
  248. * At the same time, the companion_addr shouldn't equal to client
  249. * address.
  250. */
  251. if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
  252. chip->companion_addr = pdata->companion_addr;
  253. chip->companion = i2c_new_dummy(chip->client->adapter,
  254. chip->companion_addr);
  255. i2c_set_clientdata(chip->companion, chip);
  256. }
  257. pm860x_device_init(chip, pdata);
  258. return 0;
  259. }
  260. static int __devexit pm860x_remove(struct i2c_client *client)
  261. {
  262. struct pm860x_chip *chip = i2c_get_clientdata(client);
  263. pm860x_device_exit(chip);
  264. i2c_unregister_device(chip->companion);
  265. kfree(chip);
  266. return 0;
  267. }
  268. static struct i2c_driver pm860x_driver = {
  269. .driver = {
  270. .name = "88PM860x",
  271. .owner = THIS_MODULE,
  272. },
  273. .probe = pm860x_probe,
  274. .remove = __devexit_p(pm860x_remove),
  275. .id_table = pm860x_id_table,
  276. };
  277. static int __init pm860x_i2c_init(void)
  278. {
  279. int ret;
  280. ret = i2c_add_driver(&pm860x_driver);
  281. if (ret != 0)
  282. pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
  283. return ret;
  284. }
  285. subsys_initcall(pm860x_i2c_init);
  286. static void __exit pm860x_i2c_exit(void)
  287. {
  288. i2c_del_driver(&pm860x_driver);
  289. }
  290. module_exit(pm860x_i2c_exit);
  291. MODULE_DESCRIPTION("I2C Driver for Marvell 88PM860x");
  292. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  293. MODULE_LICENSE("GPL");