1 // SPDX-License-Identifier: GPL-2.0
3 * hdm_i2c.c - Hardware Dependent Module for I2C Interface
5 * Copyright (C) 2013-2015, Microchip Technology Germany II GmbH & Co. KG
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/err.h>
18 #include "most/core.h"
20 enum { CH_RX, CH_TX, NUM_CHANNELS };
22 #define MAX_BUFFERS_CONTROL 32
23 #define MAX_BUF_SIZE_CONTROL 256
26 * list_first_mbo - get the first mbo from a list
27 * @ptr: the list head to take the mbo from.
29 #define list_first_mbo(ptr) \
30 list_first_entry(ptr, struct mbo, list)
32 /* IRQ / Polling option */
33 static bool polling_req;
34 module_param(polling_req, bool, 0444);
35 MODULE_PARM_DESC(polling_req, "Request Polling. Default = 0 (use irq)");
38 static int scan_rate = 100;
39 module_param(scan_rate, int, 0644);
40 MODULE_PARM_DESC(scan_rate, "Polling rate in times/sec. Default = 100");
43 bool is_open[NUM_CHANNELS];
45 struct most_interface most_iface;
46 struct most_channel_capability capabilities[NUM_CHANNELS];
47 struct i2c_client *client;
49 struct delayed_work dwork;
50 wait_queue_head_t waitq;
51 struct list_head list;
52 struct mutex list_mutex;
57 #define to_hdm(iface) container_of(iface, struct hdm_i2c, most_iface)
60 * configure_channel - called from MOST core to configure a channel
61 * @iface: interface the channel belongs to
62 * @channel: channel to be configured
63 * @channel_config: structure that holds the configuration information
65 * Return 0 on success, negative on failure.
67 * Receives configuration information from MOST core and initialize the
68 * corresponding channel.
70 static int configure_channel(struct most_interface *most_iface,
72 struct most_channel_config *channel_config)
74 struct hdm_i2c *dev = to_hdm(most_iface);
76 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
77 BUG_ON(dev->is_open[ch_idx]);
79 if (channel_config->data_type != MOST_CH_CONTROL) {
80 pr_err("bad data type for channel %d\n", ch_idx);
84 if (channel_config->direction != dev->capabilities[ch_idx].direction) {
85 pr_err("bad direction for channel %d\n", ch_idx);
89 if ((channel_config->direction == MOST_CH_RX) && (dev->polling_mode)) {
90 schedule_delayed_work(&dev->rx.dwork,
91 msecs_to_jiffies(MSEC_PER_SEC / 4));
93 dev->is_open[ch_idx] = true;
99 * enqueue - called from MOST core to enqueue a buffer for data transfer
100 * @iface: intended interface
101 * @channel: ID of the channel the buffer is intended for
102 * @mbo: pointer to the buffer object
104 * Return 0 on success, negative on failure.
106 * Transmit the data over I2C if it is a "write" request or push the buffer into
107 * list if it is an "read" request
109 static int enqueue(struct most_interface *most_iface,
110 int ch_idx, struct mbo *mbo)
112 struct hdm_i2c *dev = to_hdm(most_iface);
115 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
116 BUG_ON(!dev->is_open[ch_idx]);
118 if (ch_idx == CH_RX) {
120 mutex_lock(&dev->rx.list_mutex);
121 list_add_tail(&mbo->list, &dev->rx.list);
122 mutex_unlock(&dev->rx.list_mutex);
123 wake_up_interruptible(&dev->rx.waitq);
126 ret = i2c_master_send(dev->client, mbo->virt_address,
129 mbo->processed_length = 0;
130 mbo->status = MBO_E_INVAL;
132 mbo->processed_length = mbo->buffer_length;
133 mbo->status = MBO_SUCCESS;
142 * poison_channel - called from MOST core to poison buffers of a channel
143 * @iface: pointer to the interface the channel to be poisoned belongs to
144 * @channel_id: corresponding channel ID
146 * Return 0 on success, negative on failure.
148 * If channel direction is RX, complete the buffers in list with
151 static int poison_channel(struct most_interface *most_iface,
154 struct hdm_i2c *dev = to_hdm(most_iface);
157 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
158 BUG_ON(!dev->is_open[ch_idx]);
160 dev->is_open[ch_idx] = false;
162 if (ch_idx == CH_RX) {
163 mutex_lock(&dev->rx.list_mutex);
164 while (!list_empty(&dev->rx.list)) {
165 mbo = list_first_mbo(&dev->rx.list);
166 list_del(&mbo->list);
167 mutex_unlock(&dev->rx.list_mutex);
169 mbo->processed_length = 0;
170 mbo->status = MBO_E_CLOSE;
173 mutex_lock(&dev->rx.list_mutex);
175 mutex_unlock(&dev->rx.list_mutex);
176 wake_up_interruptible(&dev->rx.waitq);
182 static void do_rx_work(struct hdm_i2c *dev)
185 unsigned char msg[MAX_BUF_SIZE_CONTROL];
186 int ret, ch_idx = CH_RX;
189 /* Read PML (2 bytes) */
190 ret = i2c_master_recv(dev->client, msg, 2);
192 pr_err("Failed to receive PML\n");
196 pml = (msg[0] << 8) | msg[1];
202 /* Read the whole message, including PML */
203 ret = i2c_master_recv(dev->client, msg, data_size);
205 pr_err("Failed to receive a Port Message\n");
210 /* Conditions to wait for: poisoned channel or free buffer
211 * available for reading
213 if (wait_event_interruptible(dev->rx.waitq,
214 !dev->is_open[ch_idx] ||
215 !list_empty(&dev->rx.list))) {
216 pr_err("wait_event_interruptible() failed\n");
220 if (!dev->is_open[ch_idx])
223 mutex_lock(&dev->rx.list_mutex);
225 /* list may be empty if poison or remove is called */
226 if (!list_empty(&dev->rx.list))
229 mutex_unlock(&dev->rx.list_mutex);
232 mbo = list_first_mbo(&dev->rx.list);
233 list_del(&mbo->list);
234 mutex_unlock(&dev->rx.list_mutex);
236 mbo->processed_length = min(data_size, mbo->buffer_length);
237 memcpy(mbo->virt_address, msg, mbo->processed_length);
238 mbo->status = MBO_SUCCESS;
243 * pending_rx_work - Read pending messages through I2C
244 * @work: definition of this work item
246 * Invoked by the Interrupt Service Routine, most_irq_handler()
248 static void pending_rx_work(struct work_struct *work)
250 struct hdm_i2c *dev = container_of(work, struct hdm_i2c, rx.dwork.work);
254 if (dev->polling_mode) {
255 if (dev->is_open[CH_RX])
256 schedule_delayed_work(&dev->rx.dwork,
257 msecs_to_jiffies(MSEC_PER_SEC
260 enable_irq(dev->client->irq);
265 * most_irq_handler - Interrupt Service Routine
267 * @_dev: private data
269 * Schedules a delayed work
271 * By default the interrupt line behavior is Active Low. Once an interrupt is
272 * generated by the device, until driver clears the interrupt (by reading
273 * the PMP message), device keeps the interrupt line in low state. Since i2c
274 * read is done in work queue, the interrupt line must be disabled temporarily
275 * to avoid ISR being called repeatedly. Re-enable the interrupt in workqueue,
276 * after reading the message.
278 * Note: If we use the interrupt line in Falling edge mode, there is a
279 * possibility to miss interrupts when ISR is getting executed.
282 static irqreturn_t most_irq_handler(int irq, void *_dev)
284 struct hdm_i2c *dev = _dev;
286 disable_irq_nosync(irq);
288 schedule_delayed_work(&dev->rx.dwork, 0);
294 * i2c_probe - i2c probe handler
295 * @client: i2c client device structure
296 * @id: i2c client device id
298 * Return 0 on success, negative on failure.
300 * Register the i2c client device as a MOST interface
302 static int i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
306 struct kobject *kobj;
308 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
312 /* ID format: i2c-<bus>-<address> */
313 snprintf(dev->name, sizeof(dev->name), "i2c-%d-%04x",
314 client->adapter->nr, client->addr);
316 for (i = 0; i < NUM_CHANNELS; i++) {
317 dev->is_open[i] = false;
318 dev->capabilities[i].data_type = MOST_CH_CONTROL;
319 dev->capabilities[i].num_buffers_packet = MAX_BUFFERS_CONTROL;
320 dev->capabilities[i].buffer_size_packet = MAX_BUF_SIZE_CONTROL;
322 dev->capabilities[CH_RX].direction = MOST_CH_RX;
323 dev->capabilities[CH_RX].name_suffix = "rx";
324 dev->capabilities[CH_TX].direction = MOST_CH_TX;
325 dev->capabilities[CH_TX].name_suffix = "tx";
327 dev->most_iface.interface = ITYPE_I2C;
328 dev->most_iface.description = dev->name;
329 dev->most_iface.num_channels = NUM_CHANNELS;
330 dev->most_iface.channel_vector = dev->capabilities;
331 dev->most_iface.configure = configure_channel;
332 dev->most_iface.enqueue = enqueue;
333 dev->most_iface.poison_channel = poison_channel;
335 INIT_LIST_HEAD(&dev->rx.list);
336 mutex_init(&dev->rx.list_mutex);
337 init_waitqueue_head(&dev->rx.waitq);
339 INIT_DELAYED_WORK(&dev->rx.dwork, pending_rx_work);
341 dev->client = client;
342 i2c_set_clientdata(client, dev);
344 kobj = most_register_interface(&dev->most_iface);
346 pr_err("Failed to register i2c as a MOST interface\n");
348 return PTR_ERR(kobj);
351 dev->polling_mode = polling_req || client->irq <= 0;
352 if (!dev->polling_mode) {
353 pr_info("Requesting IRQ: %d\n", client->irq);
354 ret = request_irq(client->irq, most_irq_handler, 0,
357 pr_info("IRQ request failed: %d, falling back to polling\n",
359 dev->polling_mode = true;
363 if (dev->polling_mode)
364 pr_info("Using polling at rate: %d times/sec\n", scan_rate);
370 * i2c_remove - i2c remove handler
371 * @client: i2c client device structure
373 * Return 0 on success.
375 * Unregister the i2c client device as a MOST interface
377 static int i2c_remove(struct i2c_client *client)
379 struct hdm_i2c *dev = i2c_get_clientdata(client);
382 if (!dev->polling_mode)
383 free_irq(client->irq, dev);
385 most_deregister_interface(&dev->most_iface);
387 for (i = 0 ; i < NUM_CHANNELS; i++)
389 poison_channel(&dev->most_iface, i);
390 cancel_delayed_work_sync(&dev->rx.dwork);
396 static const struct i2c_device_id i2c_id[] = {
398 { }, /* Terminating entry */
401 MODULE_DEVICE_TABLE(i2c, i2c_id);
403 static struct i2c_driver i2c_driver = {
408 .remove = i2c_remove,
412 module_i2c_driver(i2c_driver);
414 MODULE_AUTHOR("Jain Roy Ambi <JainRoy.Ambi@microchip.com>");
415 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
416 MODULE_DESCRIPTION("I2C Hardware Dependent Module");
417 MODULE_LICENSE("GPL");