hdm_i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * hdm_i2c.c - Hardware Dependent Module for I2C Interface
  3. *
  4. * Copyright (C) 2013-2015, Microchip Technology Germany II GmbH & Co. KG
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * This file is licensed under GPLv2.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/i2c.h>
  18. #include <linux/sched.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/err.h>
  21. #include <mostcore.h>
  22. enum { CH_RX, CH_TX, NUM_CHANNELS };
  23. #define MAX_BUFFERS_CONTROL 32
  24. #define MAX_BUF_SIZE_CONTROL 256
  25. /**
  26. * list_first_mbo - get the first mbo from a list
  27. * @ptr: the list head to take the mbo from.
  28. */
  29. #define list_first_mbo(ptr) \
  30. list_first_entry(ptr, struct mbo, list)
  31. /* IRQ / Polling option */
  32. static bool polling_req;
  33. module_param(polling_req, bool, S_IRUGO);
  34. MODULE_PARM_DESC(polling_req, "Request Polling. Default = 0 (use irq)");
  35. /* Polling Rate */
  36. static int scan_rate = 100;
  37. module_param(scan_rate, int, 0644);
  38. MODULE_PARM_DESC(scan_rate, "Polling rate in times/sec. Default = 100");
  39. struct hdm_i2c {
  40. bool is_open[NUM_CHANNELS];
  41. bool polling_mode;
  42. struct most_interface most_iface;
  43. struct most_channel_capability capabilities[NUM_CHANNELS];
  44. struct i2c_client *client;
  45. struct rx {
  46. struct delayed_work dwork;
  47. wait_queue_head_t waitq;
  48. struct list_head list;
  49. struct mutex list_mutex;
  50. } rx;
  51. char name[64];
  52. };
  53. #define to_hdm(iface) container_of(iface, struct hdm_i2c, most_iface)
  54. /**
  55. * configure_channel - called from MOST core to configure a channel
  56. * @iface: interface the channel belongs to
  57. * @channel: channel to be configured
  58. * @channel_config: structure that holds the configuration information
  59. *
  60. * Return 0 on success, negative on failure.
  61. *
  62. * Receives configuration information from MOST core and initialize the
  63. * corresponding channel.
  64. */
  65. static int configure_channel(struct most_interface *most_iface,
  66. int ch_idx,
  67. struct most_channel_config *channel_config)
  68. {
  69. struct hdm_i2c *dev = to_hdm(most_iface);
  70. BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
  71. BUG_ON(dev->is_open[ch_idx]);
  72. if (channel_config->data_type != MOST_CH_CONTROL) {
  73. pr_err("bad data type for channel %d\n", ch_idx);
  74. return -EPERM;
  75. }
  76. if (channel_config->direction != dev->capabilities[ch_idx].direction) {
  77. pr_err("bad direction for channel %d\n", ch_idx);
  78. return -EPERM;
  79. }
  80. if ((channel_config->direction == MOST_CH_RX) && (dev->polling_mode)) {
  81. schedule_delayed_work(&dev->rx.dwork,
  82. msecs_to_jiffies(MSEC_PER_SEC / 4));
  83. }
  84. dev->is_open[ch_idx] = true;
  85. return 0;
  86. }
  87. /**
  88. * enqueue - called from MOST core to enqueue a buffer for data transfer
  89. * @iface: intended interface
  90. * @channel: ID of the channel the buffer is intended for
  91. * @mbo: pointer to the buffer object
  92. *
  93. * Return 0 on success, negative on failure.
  94. *
  95. * Transmit the data over I2C if it is a "write" request or push the buffer into
  96. * list if it is an "read" request
  97. */
  98. static int enqueue(struct most_interface *most_iface,
  99. int ch_idx, struct mbo *mbo)
  100. {
  101. struct hdm_i2c *dev = to_hdm(most_iface);
  102. int ret;
  103. BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
  104. BUG_ON(!dev->is_open[ch_idx]);
  105. if (ch_idx == CH_RX) {
  106. /* RX */
  107. mutex_lock(&dev->rx.list_mutex);
  108. list_add_tail(&mbo->list, &dev->rx.list);
  109. mutex_unlock(&dev->rx.list_mutex);
  110. wake_up_interruptible(&dev->rx.waitq);
  111. } else {
  112. /* TX */
  113. ret = i2c_master_send(dev->client, mbo->virt_address,
  114. mbo->buffer_length);
  115. if (ret <= 0) {
  116. mbo->processed_length = 0;
  117. mbo->status = MBO_E_INVAL;
  118. } else {
  119. mbo->processed_length = mbo->buffer_length;
  120. mbo->status = MBO_SUCCESS;
  121. }
  122. mbo->complete(mbo);
  123. }
  124. return 0;
  125. }
  126. /**
  127. * poison_channel - called from MOST core to poison buffers of a channel
  128. * @iface: pointer to the interface the channel to be poisoned belongs to
  129. * @channel_id: corresponding channel ID
  130. *
  131. * Return 0 on success, negative on failure.
  132. *
  133. * If channel direction is RX, complete the buffers in list with
  134. * status MBO_E_CLOSE
  135. */
  136. static int poison_channel(struct most_interface *most_iface,
  137. int ch_idx)
  138. {
  139. struct hdm_i2c *dev = to_hdm(most_iface);
  140. struct mbo *mbo;
  141. BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
  142. BUG_ON(!dev->is_open[ch_idx]);
  143. dev->is_open[ch_idx] = false;
  144. if (ch_idx == CH_RX) {
  145. mutex_lock(&dev->rx.list_mutex);
  146. while (!list_empty(&dev->rx.list)) {
  147. mbo = list_first_mbo(&dev->rx.list);
  148. list_del(&mbo->list);
  149. mutex_unlock(&dev->rx.list_mutex);
  150. mbo->processed_length = 0;
  151. mbo->status = MBO_E_CLOSE;
  152. mbo->complete(mbo);
  153. mutex_lock(&dev->rx.list_mutex);
  154. }
  155. mutex_unlock(&dev->rx.list_mutex);
  156. wake_up_interruptible(&dev->rx.waitq);
  157. }
  158. return 0;
  159. }
  160. static void request_netinfo(struct most_interface *most_iface,
  161. int ch_idx)
  162. {
  163. pr_info("request_netinfo()\n");
  164. }
  165. static void do_rx_work(struct hdm_i2c *dev)
  166. {
  167. struct mbo *mbo;
  168. unsigned char msg[MAX_BUF_SIZE_CONTROL];
  169. int ret, ch_idx = CH_RX;
  170. u16 pml, data_size;
  171. /* Read PML (2 bytes) */
  172. ret = i2c_master_recv(dev->client, msg, 2);
  173. if (ret <= 0) {
  174. pr_err("Failed to receive PML\n");
  175. return;
  176. }
  177. pml = (msg[0] << 8) | msg[1];
  178. if (!pml)
  179. return;
  180. data_size = pml + 2;
  181. /* Read the whole message, including PML */
  182. ret = i2c_master_recv(dev->client, msg, data_size);
  183. if (ret <= 0) {
  184. pr_err("Failed to receive a Port Message\n");
  185. return;
  186. }
  187. for (;;) {
  188. /* Conditions to wait for: poisoned channel or free buffer
  189. * available for reading
  190. */
  191. if (wait_event_interruptible(dev->rx.waitq,
  192. !dev->is_open[ch_idx] ||
  193. !list_empty(&dev->rx.list))) {
  194. pr_err("wait_event_interruptible() failed\n");
  195. return;
  196. }
  197. if (!dev->is_open[ch_idx])
  198. return;
  199. mutex_lock(&dev->rx.list_mutex);
  200. /* list may be empty if poison or remove is called */
  201. if (!list_empty(&dev->rx.list))
  202. break;
  203. mutex_unlock(&dev->rx.list_mutex);
  204. }
  205. mbo = list_first_mbo(&dev->rx.list);
  206. list_del(&mbo->list);
  207. mutex_unlock(&dev->rx.list_mutex);
  208. mbo->processed_length = min(data_size, mbo->buffer_length);
  209. memcpy(mbo->virt_address, msg, mbo->processed_length);
  210. mbo->status = MBO_SUCCESS;
  211. mbo->complete(mbo);
  212. }
  213. /**
  214. * pending_rx_work - Read pending messages through I2C
  215. * @work: definition of this work item
  216. *
  217. * Invoked by the Interrupt Service Routine, most_irq_handler()
  218. */
  219. static void pending_rx_work(struct work_struct *work)
  220. {
  221. struct hdm_i2c *dev = container_of(work, struct hdm_i2c, rx.dwork.work);
  222. do_rx_work(dev);
  223. if (dev->polling_mode) {
  224. if (dev->is_open[CH_RX])
  225. schedule_delayed_work(&dev->rx.dwork,
  226. msecs_to_jiffies(MSEC_PER_SEC
  227. / scan_rate));
  228. } else {
  229. enable_irq(dev->client->irq);
  230. }
  231. }
  232. /*
  233. * most_irq_handler - Interrupt Service Routine
  234. * @irq: irq number
  235. * @_dev: private data
  236. *
  237. * Schedules a delayed work
  238. *
  239. * By default the interrupt line behavior is Active Low. Once an interrupt is
  240. * generated by the device, until driver clears the interrupt (by reading
  241. * the PMP message), device keeps the interrupt line in low state. Since i2c
  242. * read is done in work queue, the interrupt line must be disabled temporarily
  243. * to avoid ISR being called repeatedly. Re-enable the interrupt in workqueue,
  244. * after reading the message.
  245. *
  246. * Note: If we use the interrupt line in Falling edge mode, there is a
  247. * possibility to miss interrupts when ISR is getting executed.
  248. *
  249. */
  250. static irqreturn_t most_irq_handler(int irq, void *_dev)
  251. {
  252. struct hdm_i2c *dev = _dev;
  253. disable_irq_nosync(irq);
  254. schedule_delayed_work(&dev->rx.dwork, 0);
  255. return IRQ_HANDLED;
  256. }
  257. /*
  258. * i2c_probe - i2c probe handler
  259. * @client: i2c client device structure
  260. * @id: i2c client device id
  261. *
  262. * Return 0 on success, negative on failure.
  263. *
  264. * Register the i2c client device as a MOST interface
  265. */
  266. static int i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  267. {
  268. struct hdm_i2c *dev;
  269. int ret, i;
  270. struct kobject *kobj;
  271. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  272. if (!dev)
  273. return -ENOMEM;
  274. /* ID format: i2c-<bus>-<address> */
  275. snprintf(dev->name, sizeof(dev->name), "i2c-%d-%04x",
  276. client->adapter->nr, client->addr);
  277. for (i = 0; i < NUM_CHANNELS; i++) {
  278. dev->is_open[i] = false;
  279. dev->capabilities[i].data_type = MOST_CH_CONTROL;
  280. dev->capabilities[i].num_buffers_packet = MAX_BUFFERS_CONTROL;
  281. dev->capabilities[i].buffer_size_packet = MAX_BUF_SIZE_CONTROL;
  282. }
  283. dev->capabilities[CH_RX].direction = MOST_CH_RX;
  284. dev->capabilities[CH_RX].name_suffix = "rx";
  285. dev->capabilities[CH_TX].direction = MOST_CH_TX;
  286. dev->capabilities[CH_TX].name_suffix = "tx";
  287. dev->most_iface.interface = ITYPE_I2C;
  288. dev->most_iface.description = dev->name;
  289. dev->most_iface.num_channels = NUM_CHANNELS;
  290. dev->most_iface.channel_vector = dev->capabilities;
  291. dev->most_iface.configure = configure_channel;
  292. dev->most_iface.enqueue = enqueue;
  293. dev->most_iface.poison_channel = poison_channel;
  294. dev->most_iface.request_netinfo = request_netinfo;
  295. INIT_LIST_HEAD(&dev->rx.list);
  296. mutex_init(&dev->rx.list_mutex);
  297. init_waitqueue_head(&dev->rx.waitq);
  298. INIT_DELAYED_WORK(&dev->rx.dwork, pending_rx_work);
  299. dev->client = client;
  300. i2c_set_clientdata(client, dev);
  301. kobj = most_register_interface(&dev->most_iface);
  302. if (IS_ERR(kobj)) {
  303. pr_err("Failed to register i2c as a MOST interface\n");
  304. kfree(dev);
  305. return PTR_ERR(kobj);
  306. }
  307. dev->polling_mode = polling_req || client->irq <= 0;
  308. if (!dev->polling_mode) {
  309. pr_info("Requesting IRQ: %d\n", client->irq);
  310. ret = request_irq(client->irq, most_irq_handler, 0,
  311. client->name, dev);
  312. if (ret) {
  313. pr_info("IRQ request failed: %d, falling back to polling\n",
  314. ret);
  315. dev->polling_mode = true;
  316. }
  317. }
  318. if (dev->polling_mode)
  319. pr_info("Using polling at rate: %d times/sec\n", scan_rate);
  320. return 0;
  321. }
  322. /*
  323. * i2c_remove - i2c remove handler
  324. * @client: i2c client device structure
  325. *
  326. * Return 0 on success.
  327. *
  328. * Unregister the i2c client device as a MOST interface
  329. */
  330. static int i2c_remove(struct i2c_client *client)
  331. {
  332. struct hdm_i2c *dev = i2c_get_clientdata(client);
  333. int i;
  334. if (!dev->polling_mode)
  335. free_irq(client->irq, dev);
  336. most_deregister_interface(&dev->most_iface);
  337. for (i = 0 ; i < NUM_CHANNELS; i++)
  338. if (dev->is_open[i])
  339. poison_channel(&dev->most_iface, i);
  340. cancel_delayed_work_sync(&dev->rx.dwork);
  341. kfree(dev);
  342. return 0;
  343. }
  344. static const struct i2c_device_id i2c_id[] = {
  345. { "most_i2c", 0 },
  346. { }, /* Terminating entry */
  347. };
  348. MODULE_DEVICE_TABLE(i2c, i2c_id);
  349. static struct i2c_driver i2c_driver = {
  350. .driver = {
  351. .name = "hdm_i2c",
  352. },
  353. .probe = i2c_probe,
  354. .remove = i2c_remove,
  355. .id_table = i2c_id,
  356. };
  357. module_i2c_driver(i2c_driver);
  358. MODULE_AUTHOR("Jain Roy Ambi <JainRoy.Ambi@microchip.com>");
  359. MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
  360. MODULE_DESCRIPTION("I2C Hardware Dependent Module");
  361. MODULE_LICENSE("GPL");