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>
16 #include <linux/most.h>
18 enum { CH_RX, CH_TX, NUM_CHANNELS };
20 #define MAX_BUFFERS_CONTROL 32
21 #define MAX_BUF_SIZE_CONTROL 256
24 * list_first_mbo - get the first mbo from a list
25 * @ptr: the list head to take the mbo from.
27 #define list_first_mbo(ptr) \
28 list_first_entry(ptr, struct mbo, list)
30 static unsigned int polling_rate;
31 module_param(polling_rate, uint, 0644);
32 MODULE_PARM_DESC(polling_rate, "Polling rate [Hz]. Default = 0 (use IRQ)");
35 struct most_interface most_iface;
36 struct most_channel_capability capabilities[NUM_CHANNELS];
37 struct i2c_client *client;
39 struct delayed_work dwork;
40 struct list_head list;
47 static inline struct hdm_i2c *to_hdm(struct most_interface *iface)
49 return container_of(iface, struct hdm_i2c, most_iface);
52 static irqreturn_t most_irq_handler(int, void *);
53 static void pending_rx_work(struct work_struct *);
56 * configure_channel - called from MOST core to configure a channel
57 * @most_iface: interface the channel belongs to
58 * @ch_idx: channel to be configured
59 * @channel_config: structure that holds the configuration information
61 * Return 0 on success, negative on failure.
63 * Receives configuration information from MOST core and initialize the
64 * corresponding channel.
66 static int configure_channel(struct most_interface *most_iface,
68 struct most_channel_config *channel_config)
71 struct hdm_i2c *dev = to_hdm(most_iface);
72 unsigned int delay, pr;
74 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
76 if (channel_config->data_type != MOST_CH_CONTROL) {
77 pr_err("bad data type for channel %d\n", ch_idx);
81 if (channel_config->direction != dev->capabilities[ch_idx].direction) {
82 pr_err("bad direction for channel %d\n", ch_idx);
86 if (channel_config->direction == MOST_CH_RX) {
88 if (dev->client->irq <= 0) {
89 pr_err("bad irq: %d\n", dev->client->irq);
92 dev->rx.int_disabled = false;
93 ret = request_irq(dev->client->irq, most_irq_handler, 0,
94 dev->client->name, dev);
96 pr_err("request_irq(%d) failed: %d\n",
97 dev->client->irq, ret);
101 delay = msecs_to_jiffies(MSEC_PER_SEC / polling_rate);
102 dev->rx.delay = delay ? delay : 1;
103 pr = MSEC_PER_SEC / jiffies_to_msecs(dev->rx.delay);
104 pr_info("polling rate is %u Hz\n", pr);
112 * enqueue - called from MOST core to enqueue a buffer for data transfer
113 * @most_iface: intended interface
114 * @ch_idx: ID of the channel the buffer is intended for
115 * @mbo: pointer to the buffer object
117 * Return 0 on success, negative on failure.
119 * Transmit the data over I2C if it is a "write" request or push the buffer into
120 * list if it is an "read" request
122 static int enqueue(struct most_interface *most_iface,
123 int ch_idx, struct mbo *mbo)
125 struct hdm_i2c *dev = to_hdm(most_iface);
128 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
130 if (ch_idx == CH_RX) {
133 disable_irq(dev->client->irq);
134 cancel_delayed_work_sync(&dev->rx.dwork);
135 list_add_tail(&mbo->list, &dev->rx.list);
136 if (dev->rx.int_disabled || polling_rate)
137 pending_rx_work(&dev->rx.dwork.work);
139 enable_irq(dev->client->irq);
142 ret = i2c_master_send(dev->client, mbo->virt_address,
145 mbo->processed_length = 0;
146 mbo->status = MBO_E_INVAL;
148 mbo->processed_length = mbo->buffer_length;
149 mbo->status = MBO_SUCCESS;
158 * poison_channel - called from MOST core to poison buffers of a channel
159 * @most_iface: pointer to the interface the channel to be poisoned belongs to
160 * @ch_idx: corresponding channel ID
162 * Return 0 on success, negative on failure.
164 * If channel direction is RX, complete the buffers in list with
167 static int poison_channel(struct most_interface *most_iface,
170 struct hdm_i2c *dev = to_hdm(most_iface);
173 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
175 if (ch_idx == CH_RX) {
177 free_irq(dev->client->irq, dev);
178 cancel_delayed_work_sync(&dev->rx.dwork);
180 while (!list_empty(&dev->rx.list)) {
181 mbo = list_first_mbo(&dev->rx.list);
182 list_del(&mbo->list);
184 mbo->processed_length = 0;
185 mbo->status = MBO_E_CLOSE;
193 static void do_rx_work(struct hdm_i2c *dev)
196 unsigned char msg[MAX_BUF_SIZE_CONTROL];
200 /* Read PML (2 bytes) */
201 ret = i2c_master_recv(dev->client, msg, 2);
203 pr_err("Failed to receive PML\n");
207 pml = (msg[0] << 8) | msg[1];
213 /* Read the whole message, including PML */
214 ret = i2c_master_recv(dev->client, msg, data_size);
216 pr_err("Failed to receive a Port Message\n");
220 mbo = list_first_mbo(&dev->rx.list);
221 list_del(&mbo->list);
223 mbo->processed_length = min(data_size, mbo->buffer_length);
224 memcpy(mbo->virt_address, msg, mbo->processed_length);
225 mbo->status = MBO_SUCCESS;
230 * pending_rx_work - Read pending messages through I2C
231 * @work: definition of this work item
233 * Invoked by the Interrupt Service Routine, most_irq_handler()
235 static void pending_rx_work(struct work_struct *work)
237 struct hdm_i2c *dev = container_of(work, struct hdm_i2c, rx.dwork.work);
239 if (list_empty(&dev->rx.list))
245 schedule_delayed_work(&dev->rx.dwork, dev->rx.delay);
247 dev->rx.int_disabled = false;
248 enable_irq(dev->client->irq);
253 * most_irq_handler - Interrupt Service Routine
255 * @_dev: private data
257 * Schedules a delayed work
259 * By default the interrupt line behavior is Active Low. Once an interrupt is
260 * generated by the device, until driver clears the interrupt (by reading
261 * the PMP message), device keeps the interrupt line in low state. Since i2c
262 * read is done in work queue, the interrupt line must be disabled temporarily
263 * to avoid ISR being called repeatedly. Re-enable the interrupt in workqueue,
264 * after reading the message.
266 * Note: If we use the interrupt line in Falling edge mode, there is a
267 * possibility to miss interrupts when ISR is getting executed.
270 static irqreturn_t most_irq_handler(int irq, void *_dev)
272 struct hdm_i2c *dev = _dev;
274 disable_irq_nosync(irq);
275 dev->rx.int_disabled = true;
276 schedule_delayed_work(&dev->rx.dwork, 0);
282 * i2c_probe - i2c probe handler
283 * @client: i2c client device structure
284 * @id: i2c client device id
286 * Return 0 on success, negative on failure.
288 * Register the i2c client device as a MOST interface
290 static int i2c_probe(struct i2c_client *client)
295 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
299 /* ID format: i2c-<bus>-<address> */
300 snprintf(dev->name, sizeof(dev->name), "i2c-%d-%04x",
301 client->adapter->nr, client->addr);
303 for (i = 0; i < NUM_CHANNELS; i++) {
304 dev->capabilities[i].data_type = MOST_CH_CONTROL;
305 dev->capabilities[i].num_buffers_packet = MAX_BUFFERS_CONTROL;
306 dev->capabilities[i].buffer_size_packet = MAX_BUF_SIZE_CONTROL;
308 dev->capabilities[CH_RX].direction = MOST_CH_RX;
309 dev->capabilities[CH_RX].name_suffix = "rx";
310 dev->capabilities[CH_TX].direction = MOST_CH_TX;
311 dev->capabilities[CH_TX].name_suffix = "tx";
313 dev->most_iface.interface = ITYPE_I2C;
314 dev->most_iface.description = dev->name;
315 dev->most_iface.num_channels = NUM_CHANNELS;
316 dev->most_iface.channel_vector = dev->capabilities;
317 dev->most_iface.configure = configure_channel;
318 dev->most_iface.enqueue = enqueue;
319 dev->most_iface.poison_channel = poison_channel;
321 INIT_LIST_HEAD(&dev->rx.list);
323 INIT_DELAYED_WORK(&dev->rx.dwork, pending_rx_work);
325 dev->client = client;
326 i2c_set_clientdata(client, dev);
328 ret = most_register_interface(&dev->most_iface);
330 pr_err("Failed to register i2c as a MOST interface\n");
339 * i2c_remove - i2c remove handler
340 * @client: i2c client device structure
342 * Return 0 on success.
344 * Unregister the i2c client device as a MOST interface
346 static void i2c_remove(struct i2c_client *client)
348 struct hdm_i2c *dev = i2c_get_clientdata(client);
350 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);
373 MODULE_DESCRIPTION("I2C Hardware Dependent Module");
374 MODULE_LICENSE("GPL");