1 // SPDX-License-Identifier: GPL-2.0
3 * 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/interrupt.h>
15 #include <linux/err.h>
17 #include "most/core.h"
19 enum { CH_RX, CH_TX, NUM_CHANNELS };
21 #define MAX_BUFFERS_CONTROL 32
22 #define MAX_BUF_SIZE_CONTROL 256
25 * list_first_mbo - get the first mbo from a list
26 * @ptr: the list head to take the mbo from.
28 #define list_first_mbo(ptr) \
29 list_first_entry(ptr, struct mbo, list)
31 static unsigned int polling_rate;
32 module_param(polling_rate, uint, 0644);
33 MODULE_PARM_DESC(polling_rate, "Polling rate [Hz]. Default = 0 (use IRQ)");
36 struct most_interface most_iface;
37 struct most_channel_capability capabilities[NUM_CHANNELS];
38 struct i2c_client *client;
40 struct delayed_work dwork;
41 struct list_head list;
48 #define to_hdm(iface) container_of(iface, struct hdm_i2c, most_iface)
50 static irqreturn_t most_irq_handler(int, void *);
51 static void pending_rx_work(struct work_struct *);
54 * configure_channel - called from MOST core to configure a channel
55 * @iface: interface the channel belongs to
56 * @channel: channel to be configured
57 * @channel_config: structure that holds the configuration information
59 * Return 0 on success, negative on failure.
61 * Receives configuration information from MOST core and initialize the
62 * corresponding channel.
64 static int configure_channel(struct most_interface *most_iface,
66 struct most_channel_config *channel_config)
69 struct hdm_i2c *dev = to_hdm(most_iface);
70 unsigned int delay, pr;
72 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
74 if (channel_config->data_type != MOST_CH_CONTROL) {
75 pr_err("bad data type for channel %d\n", ch_idx);
79 if (channel_config->direction != dev->capabilities[ch_idx].direction) {
80 pr_err("bad direction for channel %d\n", ch_idx);
84 if (channel_config->direction == MOST_CH_RX) {
86 if (dev->client->irq <= 0) {
87 pr_err("bad irq: %d\n", dev->client->irq);
90 dev->rx.int_disabled = false;
91 ret = request_irq(dev->client->irq, most_irq_handler, 0,
92 dev->client->name, dev);
94 pr_err("request_irq(%d) failed: %d\n",
95 dev->client->irq, ret);
99 delay = msecs_to_jiffies(MSEC_PER_SEC / polling_rate);
100 dev->rx.delay = delay ? delay : 1;
101 pr = MSEC_PER_SEC / jiffies_to_msecs(dev->rx.delay);
102 pr_info("polling rate is %u Hz\n", pr);
110 * enqueue - called from MOST core to enqueue a buffer for data transfer
111 * @iface: intended interface
112 * @channel: ID of the channel the buffer is intended for
113 * @mbo: pointer to the buffer object
115 * Return 0 on success, negative on failure.
117 * Transmit the data over I2C if it is a "write" request or push the buffer into
118 * list if it is an "read" request
120 static int enqueue(struct most_interface *most_iface,
121 int ch_idx, struct mbo *mbo)
123 struct hdm_i2c *dev = to_hdm(most_iface);
126 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
128 if (ch_idx == CH_RX) {
131 disable_irq(dev->client->irq);
132 cancel_delayed_work_sync(&dev->rx.dwork);
133 list_add_tail(&mbo->list, &dev->rx.list);
134 if (dev->rx.int_disabled || polling_rate)
135 pending_rx_work(&dev->rx.dwork.work);
137 enable_irq(dev->client->irq);
140 ret = i2c_master_send(dev->client, mbo->virt_address,
143 mbo->processed_length = 0;
144 mbo->status = MBO_E_INVAL;
146 mbo->processed_length = mbo->buffer_length;
147 mbo->status = MBO_SUCCESS;
156 * poison_channel - called from MOST core to poison buffers of a channel
157 * @iface: pointer to the interface the channel to be poisoned belongs to
158 * @channel_id: corresponding channel ID
160 * Return 0 on success, negative on failure.
162 * If channel direction is RX, complete the buffers in list with
165 static int poison_channel(struct most_interface *most_iface,
168 struct hdm_i2c *dev = to_hdm(most_iface);
171 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
173 if (ch_idx == CH_RX) {
175 free_irq(dev->client->irq, dev);
176 cancel_delayed_work_sync(&dev->rx.dwork);
178 while (!list_empty(&dev->rx.list)) {
179 mbo = list_first_mbo(&dev->rx.list);
180 list_del(&mbo->list);
182 mbo->processed_length = 0;
183 mbo->status = MBO_E_CLOSE;
191 static void do_rx_work(struct hdm_i2c *dev)
194 unsigned char msg[MAX_BUF_SIZE_CONTROL];
198 /* Read PML (2 bytes) */
199 ret = i2c_master_recv(dev->client, msg, 2);
201 pr_err("Failed to receive PML\n");
205 pml = (msg[0] << 8) | msg[1];
211 /* Read the whole message, including PML */
212 ret = i2c_master_recv(dev->client, msg, data_size);
214 pr_err("Failed to receive a Port Message\n");
218 mbo = list_first_mbo(&dev->rx.list);
219 list_del(&mbo->list);
221 mbo->processed_length = min(data_size, mbo->buffer_length);
222 memcpy(mbo->virt_address, msg, mbo->processed_length);
223 mbo->status = MBO_SUCCESS;
228 * pending_rx_work - Read pending messages through I2C
229 * @work: definition of this work item
231 * Invoked by the Interrupt Service Routine, most_irq_handler()
233 static void pending_rx_work(struct work_struct *work)
235 struct hdm_i2c *dev = container_of(work, struct hdm_i2c, rx.dwork.work);
237 if (list_empty(&dev->rx.list))
243 schedule_delayed_work(&dev->rx.dwork, dev->rx.delay);
245 dev->rx.int_disabled = false;
246 enable_irq(dev->client->irq);
251 * most_irq_handler - Interrupt Service Routine
253 * @_dev: private data
255 * Schedules a delayed work
257 * By default the interrupt line behavior is Active Low. Once an interrupt is
258 * generated by the device, until driver clears the interrupt (by reading
259 * the PMP message), device keeps the interrupt line in low state. Since i2c
260 * read is done in work queue, the interrupt line must be disabled temporarily
261 * to avoid ISR being called repeatedly. Re-enable the interrupt in workqueue,
262 * after reading the message.
264 * Note: If we use the interrupt line in Falling edge mode, there is a
265 * possibility to miss interrupts when ISR is getting executed.
268 static irqreturn_t most_irq_handler(int irq, void *_dev)
270 struct hdm_i2c *dev = _dev;
272 disable_irq_nosync(irq);
273 dev->rx.int_disabled = true;
274 schedule_delayed_work(&dev->rx.dwork, 0);
280 * i2c_probe - i2c probe handler
281 * @client: i2c client device structure
282 * @id: i2c client device id
284 * Return 0 on success, negative on failure.
286 * Register the i2c client device as a MOST interface
288 static int i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
293 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
297 /* ID format: i2c-<bus>-<address> */
298 snprintf(dev->name, sizeof(dev->name), "i2c-%d-%04x",
299 client->adapter->nr, client->addr);
301 for (i = 0; i < NUM_CHANNELS; i++) {
302 dev->capabilities[i].data_type = MOST_CH_CONTROL;
303 dev->capabilities[i].num_buffers_packet = MAX_BUFFERS_CONTROL;
304 dev->capabilities[i].buffer_size_packet = MAX_BUF_SIZE_CONTROL;
306 dev->capabilities[CH_RX].direction = MOST_CH_RX;
307 dev->capabilities[CH_RX].name_suffix = "rx";
308 dev->capabilities[CH_TX].direction = MOST_CH_TX;
309 dev->capabilities[CH_TX].name_suffix = "tx";
311 dev->most_iface.interface = ITYPE_I2C;
312 dev->most_iface.description = dev->name;
313 dev->most_iface.num_channels = NUM_CHANNELS;
314 dev->most_iface.channel_vector = dev->capabilities;
315 dev->most_iface.configure = configure_channel;
316 dev->most_iface.enqueue = enqueue;
317 dev->most_iface.poison_channel = poison_channel;
319 INIT_LIST_HEAD(&dev->rx.list);
321 INIT_DELAYED_WORK(&dev->rx.dwork, pending_rx_work);
323 dev->client = client;
324 i2c_set_clientdata(client, dev);
326 ret = most_register_interface(&dev->most_iface);
328 pr_err("Failed to register i2c as a MOST interface\n");
337 * i2c_remove - i2c remove handler
338 * @client: i2c client device structure
340 * Return 0 on success.
342 * Unregister the i2c client device as a MOST interface
344 static int i2c_remove(struct i2c_client *client)
346 struct hdm_i2c *dev = i2c_get_clientdata(client);
348 most_deregister_interface(&dev->most_iface);
354 static const struct i2c_device_id i2c_id[] = {
356 { }, /* Terminating entry */
359 MODULE_DEVICE_TABLE(i2c, i2c_id);
361 static struct i2c_driver i2c_driver = {
366 .remove = i2c_remove,
370 module_i2c_driver(i2c_driver);
372 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
373 MODULE_DESCRIPTION("I2C Hardware Dependent Module");
374 MODULE_LICENSE("GPL");