rmi_i2c.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) 2011-2016 Synaptics Incorporated
  3. * Copyright (c) 2011 Unixphere
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/i2c.h>
  10. #include <linux/rmi.h>
  11. #include <linux/of.h>
  12. #include <linux/delay.h>
  13. #include <linux/regulator/consumer.h>
  14. #include "rmi_driver.h"
  15. #define BUFFER_SIZE_INCREMENT 32
  16. /**
  17. * struct rmi_i2c_xport - stores information for i2c communication
  18. *
  19. * @xport: The transport interface structure
  20. *
  21. * @page_mutex: Locks current page to avoid changing pages in unexpected ways.
  22. * @page: Keeps track of the current virtual page
  23. *
  24. * @tx_buf: Buffer used for transmitting data to the sensor over i2c.
  25. * @tx_buf_size: Size of the buffer
  26. */
  27. struct rmi_i2c_xport {
  28. struct rmi_transport_dev xport;
  29. struct i2c_client *client;
  30. struct mutex page_mutex;
  31. int page;
  32. u8 *tx_buf;
  33. size_t tx_buf_size;
  34. struct regulator_bulk_data supplies[2];
  35. u32 startup_delay;
  36. };
  37. #define RMI_PAGE_SELECT_REGISTER 0xff
  38. #define RMI_I2C_PAGE(addr) (((addr) >> 8) & 0xff)
  39. /*
  40. * rmi_set_page - Set RMI page
  41. * @xport: The pointer to the rmi_transport_dev struct
  42. * @page: The new page address.
  43. *
  44. * RMI devices have 16-bit addressing, but some of the transport
  45. * implementations (like SMBus) only have 8-bit addressing. So RMI implements
  46. * a page address at 0xff of every page so we can reliable page addresses
  47. * every 256 registers.
  48. *
  49. * The page_mutex lock must be held when this function is entered.
  50. *
  51. * Returns zero on success, non-zero on failure.
  52. */
  53. static int rmi_set_page(struct rmi_i2c_xport *rmi_i2c, u8 page)
  54. {
  55. struct i2c_client *client = rmi_i2c->client;
  56. u8 txbuf[2] = {RMI_PAGE_SELECT_REGISTER, page};
  57. int retval;
  58. retval = i2c_master_send(client, txbuf, sizeof(txbuf));
  59. if (retval != sizeof(txbuf)) {
  60. dev_err(&client->dev,
  61. "%s: set page failed: %d.", __func__, retval);
  62. return (retval < 0) ? retval : -EIO;
  63. }
  64. rmi_i2c->page = page;
  65. return 0;
  66. }
  67. static int rmi_i2c_write_block(struct rmi_transport_dev *xport, u16 addr,
  68. const void *buf, size_t len)
  69. {
  70. struct rmi_i2c_xport *rmi_i2c =
  71. container_of(xport, struct rmi_i2c_xport, xport);
  72. struct i2c_client *client = rmi_i2c->client;
  73. size_t tx_size = len + 1;
  74. int retval;
  75. mutex_lock(&rmi_i2c->page_mutex);
  76. if (!rmi_i2c->tx_buf || rmi_i2c->tx_buf_size < tx_size) {
  77. if (rmi_i2c->tx_buf)
  78. devm_kfree(&client->dev, rmi_i2c->tx_buf);
  79. rmi_i2c->tx_buf_size = tx_size + BUFFER_SIZE_INCREMENT;
  80. rmi_i2c->tx_buf = devm_kzalloc(&client->dev,
  81. rmi_i2c->tx_buf_size,
  82. GFP_KERNEL);
  83. if (!rmi_i2c->tx_buf) {
  84. rmi_i2c->tx_buf_size = 0;
  85. retval = -ENOMEM;
  86. goto exit;
  87. }
  88. }
  89. rmi_i2c->tx_buf[0] = addr & 0xff;
  90. memcpy(rmi_i2c->tx_buf + 1, buf, len);
  91. if (RMI_I2C_PAGE(addr) != rmi_i2c->page) {
  92. retval = rmi_set_page(rmi_i2c, RMI_I2C_PAGE(addr));
  93. if (retval)
  94. goto exit;
  95. }
  96. retval = i2c_master_send(client, rmi_i2c->tx_buf, tx_size);
  97. if (retval == tx_size)
  98. retval = 0;
  99. else if (retval >= 0)
  100. retval = -EIO;
  101. exit:
  102. rmi_dbg(RMI_DEBUG_XPORT, &client->dev,
  103. "write %zd bytes at %#06x: %d (%*ph)\n",
  104. len, addr, retval, (int)len, buf);
  105. mutex_unlock(&rmi_i2c->page_mutex);
  106. return retval;
  107. }
  108. static int rmi_i2c_read_block(struct rmi_transport_dev *xport, u16 addr,
  109. void *buf, size_t len)
  110. {
  111. struct rmi_i2c_xport *rmi_i2c =
  112. container_of(xport, struct rmi_i2c_xport, xport);
  113. struct i2c_client *client = rmi_i2c->client;
  114. u8 addr_offset = addr & 0xff;
  115. int retval;
  116. struct i2c_msg msgs[] = {
  117. {
  118. .addr = client->addr,
  119. .len = sizeof(addr_offset),
  120. .buf = &addr_offset,
  121. },
  122. {
  123. .addr = client->addr,
  124. .flags = I2C_M_RD,
  125. .len = len,
  126. .buf = buf,
  127. },
  128. };
  129. mutex_lock(&rmi_i2c->page_mutex);
  130. if (RMI_I2C_PAGE(addr) != rmi_i2c->page) {
  131. retval = rmi_set_page(rmi_i2c, RMI_I2C_PAGE(addr));
  132. if (retval)
  133. goto exit;
  134. }
  135. retval = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  136. if (retval == ARRAY_SIZE(msgs))
  137. retval = 0; /* success */
  138. else if (retval >= 0)
  139. retval = -EIO;
  140. exit:
  141. rmi_dbg(RMI_DEBUG_XPORT, &client->dev,
  142. "read %zd bytes at %#06x: %d (%*ph)\n",
  143. len, addr, retval, (int)len, buf);
  144. mutex_unlock(&rmi_i2c->page_mutex);
  145. return retval;
  146. }
  147. static const struct rmi_transport_ops rmi_i2c_ops = {
  148. .write_block = rmi_i2c_write_block,
  149. .read_block = rmi_i2c_read_block,
  150. };
  151. #ifdef CONFIG_OF
  152. static const struct of_device_id rmi_i2c_of_match[] = {
  153. { .compatible = "syna,rmi4-i2c" },
  154. {},
  155. };
  156. MODULE_DEVICE_TABLE(of, rmi_i2c_of_match);
  157. #endif
  158. static void rmi_i2c_regulator_bulk_disable(void *data)
  159. {
  160. struct rmi_i2c_xport *rmi_i2c = data;
  161. regulator_bulk_disable(ARRAY_SIZE(rmi_i2c->supplies),
  162. rmi_i2c->supplies);
  163. }
  164. static void rmi_i2c_unregister_transport(void *data)
  165. {
  166. struct rmi_i2c_xport *rmi_i2c = data;
  167. rmi_unregister_transport_device(&rmi_i2c->xport);
  168. }
  169. static int rmi_i2c_probe(struct i2c_client *client,
  170. const struct i2c_device_id *id)
  171. {
  172. struct rmi_device_platform_data *pdata;
  173. struct rmi_device_platform_data *client_pdata =
  174. dev_get_platdata(&client->dev);
  175. struct rmi_i2c_xport *rmi_i2c;
  176. int error;
  177. rmi_i2c = devm_kzalloc(&client->dev, sizeof(struct rmi_i2c_xport),
  178. GFP_KERNEL);
  179. if (!rmi_i2c)
  180. return -ENOMEM;
  181. pdata = &rmi_i2c->xport.pdata;
  182. if (!client->dev.of_node && client_pdata)
  183. *pdata = *client_pdata;
  184. pdata->irq = client->irq;
  185. rmi_dbg(RMI_DEBUG_XPORT, &client->dev, "Probing %s.\n",
  186. dev_name(&client->dev));
  187. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  188. dev_err(&client->dev,
  189. "adapter does not support required functionality\n");
  190. return -ENODEV;
  191. }
  192. rmi_i2c->supplies[0].supply = "vdd";
  193. rmi_i2c->supplies[1].supply = "vio";
  194. error = devm_regulator_bulk_get(&client->dev,
  195. ARRAY_SIZE(rmi_i2c->supplies),
  196. rmi_i2c->supplies);
  197. if (error < 0)
  198. return error;
  199. error = regulator_bulk_enable(ARRAY_SIZE(rmi_i2c->supplies),
  200. rmi_i2c->supplies);
  201. if (error < 0)
  202. return error;
  203. error = devm_add_action_or_reset(&client->dev,
  204. rmi_i2c_regulator_bulk_disable,
  205. rmi_i2c);
  206. if (error)
  207. return error;
  208. of_property_read_u32(client->dev.of_node, "syna,startup-delay-ms",
  209. &rmi_i2c->startup_delay);
  210. msleep(rmi_i2c->startup_delay);
  211. rmi_i2c->client = client;
  212. mutex_init(&rmi_i2c->page_mutex);
  213. rmi_i2c->xport.dev = &client->dev;
  214. rmi_i2c->xport.proto_name = "i2c";
  215. rmi_i2c->xport.ops = &rmi_i2c_ops;
  216. i2c_set_clientdata(client, rmi_i2c);
  217. /*
  218. * Setting the page to zero will (a) make sure the PSR is in a
  219. * known state, and (b) make sure we can talk to the device.
  220. */
  221. error = rmi_set_page(rmi_i2c, 0);
  222. if (error) {
  223. dev_err(&client->dev, "Failed to set page select to 0\n");
  224. return error;
  225. }
  226. dev_info(&client->dev, "registering I2C-connected sensor\n");
  227. error = rmi_register_transport_device(&rmi_i2c->xport);
  228. if (error) {
  229. dev_err(&client->dev, "failed to register sensor: %d\n", error);
  230. return error;
  231. }
  232. error = devm_add_action_or_reset(&client->dev,
  233. rmi_i2c_unregister_transport,
  234. rmi_i2c);
  235. if (error)
  236. return error;
  237. return 0;
  238. }
  239. #ifdef CONFIG_PM_SLEEP
  240. static int rmi_i2c_suspend(struct device *dev)
  241. {
  242. struct i2c_client *client = to_i2c_client(dev);
  243. struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
  244. int ret;
  245. ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev, true);
  246. if (ret)
  247. dev_warn(dev, "Failed to resume device: %d\n", ret);
  248. regulator_bulk_disable(ARRAY_SIZE(rmi_i2c->supplies),
  249. rmi_i2c->supplies);
  250. return ret;
  251. }
  252. static int rmi_i2c_resume(struct device *dev)
  253. {
  254. struct i2c_client *client = to_i2c_client(dev);
  255. struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
  256. int ret;
  257. ret = regulator_bulk_enable(ARRAY_SIZE(rmi_i2c->supplies),
  258. rmi_i2c->supplies);
  259. if (ret)
  260. return ret;
  261. msleep(rmi_i2c->startup_delay);
  262. ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev, true);
  263. if (ret)
  264. dev_warn(dev, "Failed to resume device: %d\n", ret);
  265. return ret;
  266. }
  267. #endif
  268. #ifdef CONFIG_PM
  269. static int rmi_i2c_runtime_suspend(struct device *dev)
  270. {
  271. struct i2c_client *client = to_i2c_client(dev);
  272. struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
  273. int ret;
  274. ret = rmi_driver_suspend(rmi_i2c->xport.rmi_dev, false);
  275. if (ret)
  276. dev_warn(dev, "Failed to resume device: %d\n", ret);
  277. regulator_bulk_disable(ARRAY_SIZE(rmi_i2c->supplies),
  278. rmi_i2c->supplies);
  279. return 0;
  280. }
  281. static int rmi_i2c_runtime_resume(struct device *dev)
  282. {
  283. struct i2c_client *client = to_i2c_client(dev);
  284. struct rmi_i2c_xport *rmi_i2c = i2c_get_clientdata(client);
  285. int ret;
  286. ret = regulator_bulk_enable(ARRAY_SIZE(rmi_i2c->supplies),
  287. rmi_i2c->supplies);
  288. if (ret)
  289. return ret;
  290. msleep(rmi_i2c->startup_delay);
  291. ret = rmi_driver_resume(rmi_i2c->xport.rmi_dev, false);
  292. if (ret)
  293. dev_warn(dev, "Failed to resume device: %d\n", ret);
  294. return 0;
  295. }
  296. #endif
  297. static const struct dev_pm_ops rmi_i2c_pm = {
  298. SET_SYSTEM_SLEEP_PM_OPS(rmi_i2c_suspend, rmi_i2c_resume)
  299. SET_RUNTIME_PM_OPS(rmi_i2c_runtime_suspend, rmi_i2c_runtime_resume,
  300. NULL)
  301. };
  302. static const struct i2c_device_id rmi_id[] = {
  303. { "rmi4_i2c", 0 },
  304. { }
  305. };
  306. MODULE_DEVICE_TABLE(i2c, rmi_id);
  307. static struct i2c_driver rmi_i2c_driver = {
  308. .driver = {
  309. .name = "rmi4_i2c",
  310. .pm = &rmi_i2c_pm,
  311. .of_match_table = of_match_ptr(rmi_i2c_of_match),
  312. },
  313. .id_table = rmi_id,
  314. .probe = rmi_i2c_probe,
  315. };
  316. module_i2c_driver(rmi_i2c_driver);
  317. MODULE_AUTHOR("Christopher Heiny <cheiny@synaptics.com>");
  318. MODULE_AUTHOR("Andrew Duggan <aduggan@synaptics.com>");
  319. MODULE_DESCRIPTION("RMI I2C driver");
  320. MODULE_LICENSE("GPL");