88pm860x-i2c.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/err.h>
  16. #include <linux/regmap.h>
  17. #include <linux/mfd/88pm860x.h>
  18. #include <linux/slab.h>
  19. int pm860x_reg_read(struct i2c_client *i2c, int reg)
  20. {
  21. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  22. struct regmap *map = (i2c == chip->client) ? chip->regmap
  23. : chip->regmap_companion;
  24. unsigned int data;
  25. int ret;
  26. ret = regmap_read(map, reg, &data);
  27. if (ret < 0)
  28. return ret;
  29. else
  30. return (int)data;
  31. }
  32. EXPORT_SYMBOL(pm860x_reg_read);
  33. int pm860x_reg_write(struct i2c_client *i2c, int reg,
  34. unsigned char data)
  35. {
  36. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  37. struct regmap *map = (i2c == chip->client) ? chip->regmap
  38. : chip->regmap_companion;
  39. int ret;
  40. ret = regmap_write(map, reg, data);
  41. return ret;
  42. }
  43. EXPORT_SYMBOL(pm860x_reg_write);
  44. int pm860x_bulk_read(struct i2c_client *i2c, int reg,
  45. int count, unsigned char *buf)
  46. {
  47. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  48. struct regmap *map = (i2c == chip->client) ? chip->regmap
  49. : chip->regmap_companion;
  50. int ret;
  51. ret = regmap_raw_read(map, reg, buf, count);
  52. return ret;
  53. }
  54. EXPORT_SYMBOL(pm860x_bulk_read);
  55. int pm860x_bulk_write(struct i2c_client *i2c, int reg,
  56. int count, unsigned char *buf)
  57. {
  58. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  59. struct regmap *map = (i2c == chip->client) ? chip->regmap
  60. : chip->regmap_companion;
  61. int ret;
  62. ret = regmap_raw_write(map, reg, buf, count);
  63. return ret;
  64. }
  65. EXPORT_SYMBOL(pm860x_bulk_write);
  66. int pm860x_set_bits(struct i2c_client *i2c, int reg,
  67. unsigned char mask, unsigned char data)
  68. {
  69. struct pm860x_chip *chip = i2c_get_clientdata(i2c);
  70. struct regmap *map = (i2c == chip->client) ? chip->regmap
  71. : chip->regmap_companion;
  72. int ret;
  73. ret = regmap_update_bits(map, reg, mask, data);
  74. return ret;
  75. }
  76. EXPORT_SYMBOL(pm860x_set_bits);
  77. static int read_device(struct i2c_client *i2c, int reg,
  78. int bytes, void *dest)
  79. {
  80. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX + 3];
  81. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX + 2];
  82. struct i2c_adapter *adap = i2c->adapter;
  83. struct i2c_msg msg[2] = {{i2c->addr, 0, 1, msgbuf0},
  84. {i2c->addr, I2C_M_RD, 0, msgbuf1},
  85. };
  86. int num = 1, ret = 0;
  87. if (dest == NULL)
  88. return -EINVAL;
  89. msgbuf0[0] = (unsigned char)reg; /* command */
  90. msg[1].len = bytes;
  91. /* if data needs to read back, num should be 2 */
  92. if (bytes > 0)
  93. num = 2;
  94. ret = adap->algo->master_xfer(adap, msg, num);
  95. memcpy(dest, msgbuf1, bytes);
  96. if (ret < 0)
  97. return ret;
  98. return 0;
  99. }
  100. static int write_device(struct i2c_client *i2c, int reg,
  101. int bytes, void *src)
  102. {
  103. unsigned char buf[bytes + 1];
  104. struct i2c_adapter *adap = i2c->adapter;
  105. struct i2c_msg msg;
  106. int ret;
  107. buf[0] = (unsigned char)reg;
  108. memcpy(&buf[1], src, bytes);
  109. msg.addr = i2c->addr;
  110. msg.flags = 0;
  111. msg.len = bytes + 1;
  112. msg.buf = buf;
  113. ret = adap->algo->master_xfer(adap, &msg, 1);
  114. if (ret < 0)
  115. return ret;
  116. return 0;
  117. }
  118. int pm860x_page_reg_read(struct i2c_client *i2c, int reg)
  119. {
  120. unsigned char zero = 0;
  121. unsigned char data;
  122. int ret;
  123. i2c_lock_adapter(i2c->adapter);
  124. read_device(i2c, 0xFA, 0, &zero);
  125. read_device(i2c, 0xFB, 0, &zero);
  126. read_device(i2c, 0xFF, 0, &zero);
  127. ret = read_device(i2c, reg, 1, &data);
  128. if (ret >= 0)
  129. ret = (int)data;
  130. read_device(i2c, 0xFE, 0, &zero);
  131. read_device(i2c, 0xFC, 0, &zero);
  132. i2c_unlock_adapter(i2c->adapter);
  133. return ret;
  134. }
  135. EXPORT_SYMBOL(pm860x_page_reg_read);
  136. int pm860x_page_reg_write(struct i2c_client *i2c, int reg,
  137. unsigned char data)
  138. {
  139. unsigned char zero;
  140. int ret;
  141. i2c_lock_adapter(i2c->adapter);
  142. read_device(i2c, 0xFA, 0, &zero);
  143. read_device(i2c, 0xFB, 0, &zero);
  144. read_device(i2c, 0xFF, 0, &zero);
  145. ret = write_device(i2c, reg, 1, &data);
  146. read_device(i2c, 0xFE, 0, &zero);
  147. read_device(i2c, 0xFC, 0, &zero);
  148. i2c_unlock_adapter(i2c->adapter);
  149. return ret;
  150. }
  151. EXPORT_SYMBOL(pm860x_page_reg_write);
  152. int pm860x_page_bulk_read(struct i2c_client *i2c, int reg,
  153. int count, unsigned char *buf)
  154. {
  155. unsigned char zero = 0;
  156. int ret;
  157. i2c_lock_adapter(i2c->adapter);
  158. read_device(i2c, 0xfa, 0, &zero);
  159. read_device(i2c, 0xfb, 0, &zero);
  160. read_device(i2c, 0xff, 0, &zero);
  161. ret = read_device(i2c, reg, count, buf);
  162. read_device(i2c, 0xFE, 0, &zero);
  163. read_device(i2c, 0xFC, 0, &zero);
  164. i2c_unlock_adapter(i2c->adapter);
  165. return ret;
  166. }
  167. EXPORT_SYMBOL(pm860x_page_bulk_read);
  168. int pm860x_page_bulk_write(struct i2c_client *i2c, int reg,
  169. int count, unsigned char *buf)
  170. {
  171. unsigned char zero = 0;
  172. int ret;
  173. i2c_lock_adapter(i2c->adapter);
  174. read_device(i2c, 0xFA, 0, &zero);
  175. read_device(i2c, 0xFB, 0, &zero);
  176. read_device(i2c, 0xFF, 0, &zero);
  177. ret = write_device(i2c, reg, count, buf);
  178. read_device(i2c, 0xFE, 0, &zero);
  179. read_device(i2c, 0xFC, 0, &zero);
  180. i2c_unlock_adapter(i2c->adapter);
  181. i2c_unlock_adapter(i2c->adapter);
  182. return ret;
  183. }
  184. EXPORT_SYMBOL(pm860x_page_bulk_write);
  185. int pm860x_page_set_bits(struct i2c_client *i2c, int reg,
  186. unsigned char mask, unsigned char data)
  187. {
  188. unsigned char zero;
  189. unsigned char value;
  190. int ret;
  191. i2c_lock_adapter(i2c->adapter);
  192. read_device(i2c, 0xFA, 0, &zero);
  193. read_device(i2c, 0xFB, 0, &zero);
  194. read_device(i2c, 0xFF, 0, &zero);
  195. ret = read_device(i2c, reg, 1, &value);
  196. if (ret < 0)
  197. goto out;
  198. value &= ~mask;
  199. value |= data;
  200. ret = write_device(i2c, reg, 1, &value);
  201. out:
  202. read_device(i2c, 0xFE, 0, &zero);
  203. read_device(i2c, 0xFC, 0, &zero);
  204. i2c_unlock_adapter(i2c->adapter);
  205. return ret;
  206. }
  207. EXPORT_SYMBOL(pm860x_page_set_bits);
  208. static const struct i2c_device_id pm860x_id_table[] = {
  209. { "88PM860x", 0 },
  210. {}
  211. };
  212. MODULE_DEVICE_TABLE(i2c, pm860x_id_table);
  213. static int verify_addr(struct i2c_client *i2c)
  214. {
  215. unsigned short addr_8607[] = {0x30, 0x34};
  216. unsigned short addr_8606[] = {0x10, 0x11};
  217. int size, i;
  218. if (i2c == NULL)
  219. return 0;
  220. size = ARRAY_SIZE(addr_8606);
  221. for (i = 0; i < size; i++) {
  222. if (i2c->addr == *(addr_8606 + i))
  223. return CHIP_PM8606;
  224. }
  225. size = ARRAY_SIZE(addr_8607);
  226. for (i = 0; i < size; i++) {
  227. if (i2c->addr == *(addr_8607 + i))
  228. return CHIP_PM8607;
  229. }
  230. return 0;
  231. }
  232. static struct regmap_config pm860x_regmap_config = {
  233. .reg_bits = 8,
  234. .val_bits = 8,
  235. };
  236. static int __devinit pm860x_probe(struct i2c_client *client,
  237. const struct i2c_device_id *id)
  238. {
  239. struct pm860x_platform_data *pdata = client->dev.platform_data;
  240. struct pm860x_chip *chip;
  241. int ret;
  242. if (!pdata) {
  243. pr_info("No platform data in %s!\n", __func__);
  244. return -EINVAL;
  245. }
  246. chip = kzalloc(sizeof(struct pm860x_chip), GFP_KERNEL);
  247. if (chip == NULL)
  248. return -ENOMEM;
  249. chip->id = verify_addr(client);
  250. chip->regmap = regmap_init_i2c(client, &pm860x_regmap_config);
  251. if (IS_ERR(chip->regmap)) {
  252. ret = PTR_ERR(chip->regmap);
  253. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  254. ret);
  255. kfree(chip);
  256. return ret;
  257. }
  258. chip->client = client;
  259. i2c_set_clientdata(client, chip);
  260. chip->dev = &client->dev;
  261. dev_set_drvdata(chip->dev, chip);
  262. /*
  263. * Both client and companion client shares same platform driver.
  264. * Driver distinguishes them by pdata->companion_addr.
  265. * pdata->companion_addr is only assigned if companion chip exists.
  266. * At the same time, the companion_addr shouldn't equal to client
  267. * address.
  268. */
  269. if (pdata->companion_addr && (pdata->companion_addr != client->addr)) {
  270. chip->companion_addr = pdata->companion_addr;
  271. chip->companion = i2c_new_dummy(chip->client->adapter,
  272. chip->companion_addr);
  273. chip->regmap_companion = regmap_init_i2c(chip->companion,
  274. &pm860x_regmap_config);
  275. if (IS_ERR(chip->regmap_companion)) {
  276. ret = PTR_ERR(chip->regmap_companion);
  277. dev_err(&chip->companion->dev,
  278. "Failed to allocate register map: %d\n", ret);
  279. return ret;
  280. }
  281. i2c_set_clientdata(chip->companion, chip);
  282. }
  283. pm860x_device_init(chip, pdata);
  284. return 0;
  285. }
  286. static int __devexit pm860x_remove(struct i2c_client *client)
  287. {
  288. struct pm860x_chip *chip = i2c_get_clientdata(client);
  289. pm860x_device_exit(chip);
  290. if (chip->companion) {
  291. regmap_exit(chip->regmap_companion);
  292. i2c_unregister_device(chip->companion);
  293. }
  294. regmap_exit(chip->regmap);
  295. kfree(chip);
  296. return 0;
  297. }
  298. #ifdef CONFIG_PM_SLEEP
  299. static int pm860x_suspend(struct device *dev)
  300. {
  301. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  302. struct pm860x_chip *chip = i2c_get_clientdata(client);
  303. if (device_may_wakeup(dev) && chip->wakeup_flag)
  304. enable_irq_wake(chip->core_irq);
  305. return 0;
  306. }
  307. static int pm860x_resume(struct device *dev)
  308. {
  309. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  310. struct pm860x_chip *chip = i2c_get_clientdata(client);
  311. if (device_may_wakeup(dev) && chip->wakeup_flag)
  312. disable_irq_wake(chip->core_irq);
  313. return 0;
  314. }
  315. #endif
  316. static SIMPLE_DEV_PM_OPS(pm860x_pm_ops, pm860x_suspend, pm860x_resume);
  317. static struct i2c_driver pm860x_driver = {
  318. .driver = {
  319. .name = "88PM860x",
  320. .owner = THIS_MODULE,
  321. .pm = &pm860x_pm_ops,
  322. },
  323. .probe = pm860x_probe,
  324. .remove = __devexit_p(pm860x_remove),
  325. .id_table = pm860x_id_table,
  326. };
  327. static int __init pm860x_i2c_init(void)
  328. {
  329. int ret;
  330. ret = i2c_add_driver(&pm860x_driver);
  331. if (ret != 0)
  332. pr_err("Failed to register 88PM860x I2C driver: %d\n", ret);
  333. return ret;
  334. }
  335. subsys_initcall(pm860x_i2c_init);
  336. static void __exit pm860x_i2c_exit(void)
  337. {
  338. i2c_del_driver(&pm860x_driver);
  339. }
  340. module_exit(pm860x_i2c_exit);
  341. MODULE_DESCRIPTION("I2C Driver for Marvell 88PM860x");
  342. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  343. MODULE_LICENSE("GPL");