xref: /openbmc/linux/drivers/staging/most/i2c/i2c.c (revision 015d239a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * i2c.c - Hardware Dependent Module for I2C Interface
4  *
5  * Copyright (C) 2013-2015, Microchip Technology Germany II GmbH & Co. KG
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
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 
17 #include "../most.h"
18 
19 enum { CH_RX, CH_TX, NUM_CHANNELS };
20 
21 #define MAX_BUFFERS_CONTROL 32
22 #define MAX_BUF_SIZE_CONTROL 256
23 
24 /**
25  * list_first_mbo - get the first mbo from a list
26  * @ptr:	the list head to take the mbo from.
27  */
28 #define list_first_mbo(ptr) \
29 	list_first_entry(ptr, struct mbo, list)
30 
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)");
34 
35 struct hdm_i2c {
36 	struct most_interface most_iface;
37 	struct most_channel_capability capabilities[NUM_CHANNELS];
38 	struct i2c_client *client;
39 	struct rx {
40 		struct delayed_work dwork;
41 		struct list_head list;
42 		bool int_disabled;
43 		unsigned int delay;
44 	} rx;
45 	char name[64];
46 };
47 
48 #define to_hdm(iface) container_of(iface, struct hdm_i2c, most_iface)
49 
50 static irqreturn_t most_irq_handler(int, void *);
51 static void pending_rx_work(struct work_struct *);
52 
53 /**
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
58  *
59  * Return 0 on success, negative on failure.
60  *
61  * Receives configuration information from MOST core and initialize the
62  * corresponding channel.
63  */
64 static int configure_channel(struct most_interface *most_iface,
65 			     int ch_idx,
66 			     struct most_channel_config *channel_config)
67 {
68 	int ret;
69 	struct hdm_i2c *dev = to_hdm(most_iface);
70 	unsigned int delay, pr;
71 
72 	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
73 
74 	if (channel_config->data_type != MOST_CH_CONTROL) {
75 		pr_err("bad data type for channel %d\n", ch_idx);
76 		return -EPERM;
77 	}
78 
79 	if (channel_config->direction != dev->capabilities[ch_idx].direction) {
80 		pr_err("bad direction for channel %d\n", ch_idx);
81 		return -EPERM;
82 	}
83 
84 	if (channel_config->direction == MOST_CH_RX) {
85 		if (!polling_rate) {
86 			if (dev->client->irq <= 0) {
87 				pr_err("bad irq: %d\n", dev->client->irq);
88 				return -ENOENT;
89 			}
90 			dev->rx.int_disabled = false;
91 			ret = request_irq(dev->client->irq, most_irq_handler, 0,
92 					  dev->client->name, dev);
93 			if (ret) {
94 				pr_err("request_irq(%d) failed: %d\n",
95 				       dev->client->irq, ret);
96 				return ret;
97 			}
98 		} else {
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);
103 		}
104 	}
105 
106 	return 0;
107 }
108 
109 /**
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
114  *
115  * Return 0 on success, negative on failure.
116  *
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
119  */
120 static int enqueue(struct most_interface *most_iface,
121 		   int ch_idx, struct mbo *mbo)
122 {
123 	struct hdm_i2c *dev = to_hdm(most_iface);
124 	int ret;
125 
126 	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
127 
128 	if (ch_idx == CH_RX) {
129 		/* RX */
130 		if (!polling_rate)
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);
136 		if (!polling_rate)
137 			enable_irq(dev->client->irq);
138 	} else {
139 		/* TX */
140 		ret = i2c_master_send(dev->client, mbo->virt_address,
141 				      mbo->buffer_length);
142 		if (ret <= 0) {
143 			mbo->processed_length = 0;
144 			mbo->status = MBO_E_INVAL;
145 		} else {
146 			mbo->processed_length = mbo->buffer_length;
147 			mbo->status = MBO_SUCCESS;
148 		}
149 		mbo->complete(mbo);
150 	}
151 
152 	return 0;
153 }
154 
155 /**
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
159  *
160  * Return 0 on success, negative on failure.
161  *
162  * If channel direction is RX, complete the buffers in list with
163  * status MBO_E_CLOSE
164  */
165 static int poison_channel(struct most_interface *most_iface,
166 			  int ch_idx)
167 {
168 	struct hdm_i2c *dev = to_hdm(most_iface);
169 	struct mbo *mbo;
170 
171 	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
172 
173 	if (ch_idx == CH_RX) {
174 		if (!polling_rate)
175 			free_irq(dev->client->irq, dev);
176 		cancel_delayed_work_sync(&dev->rx.dwork);
177 
178 		while (!list_empty(&dev->rx.list)) {
179 			mbo = list_first_mbo(&dev->rx.list);
180 			list_del(&mbo->list);
181 
182 			mbo->processed_length = 0;
183 			mbo->status = MBO_E_CLOSE;
184 			mbo->complete(mbo);
185 		}
186 	}
187 
188 	return 0;
189 }
190 
191 static void do_rx_work(struct hdm_i2c *dev)
192 {
193 	struct mbo *mbo;
194 	unsigned char msg[MAX_BUF_SIZE_CONTROL];
195 	int ret;
196 	u16 pml, data_size;
197 
198 	/* Read PML (2 bytes) */
199 	ret = i2c_master_recv(dev->client, msg, 2);
200 	if (ret <= 0) {
201 		pr_err("Failed to receive PML\n");
202 		return;
203 	}
204 
205 	pml = (msg[0] << 8) | msg[1];
206 	if (!pml)
207 		return;
208 
209 	data_size = pml + 2;
210 
211 	/* Read the whole message, including PML */
212 	ret = i2c_master_recv(dev->client, msg, data_size);
213 	if (ret <= 0) {
214 		pr_err("Failed to receive a Port Message\n");
215 		return;
216 	}
217 
218 	mbo = list_first_mbo(&dev->rx.list);
219 	list_del(&mbo->list);
220 
221 	mbo->processed_length = min(data_size, mbo->buffer_length);
222 	memcpy(mbo->virt_address, msg, mbo->processed_length);
223 	mbo->status = MBO_SUCCESS;
224 	mbo->complete(mbo);
225 }
226 
227 /**
228  * pending_rx_work - Read pending messages through I2C
229  * @work: definition of this work item
230  *
231  * Invoked by the Interrupt Service Routine, most_irq_handler()
232  */
233 static void pending_rx_work(struct work_struct *work)
234 {
235 	struct hdm_i2c *dev = container_of(work, struct hdm_i2c, rx.dwork.work);
236 
237 	if (list_empty(&dev->rx.list))
238 		return;
239 
240 	do_rx_work(dev);
241 
242 	if (polling_rate) {
243 		schedule_delayed_work(&dev->rx.dwork, dev->rx.delay);
244 	} else {
245 		dev->rx.int_disabled = false;
246 		enable_irq(dev->client->irq);
247 	}
248 }
249 
250 /*
251  * most_irq_handler - Interrupt Service Routine
252  * @irq: irq number
253  * @_dev: private data
254  *
255  * Schedules a delayed work
256  *
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.
263  *
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.
266  *
267  */
268 static irqreturn_t most_irq_handler(int irq, void *_dev)
269 {
270 	struct hdm_i2c *dev = _dev;
271 
272 	disable_irq_nosync(irq);
273 	dev->rx.int_disabled = true;
274 	schedule_delayed_work(&dev->rx.dwork, 0);
275 
276 	return IRQ_HANDLED;
277 }
278 
279 /*
280  * i2c_probe - i2c probe handler
281  * @client: i2c client device structure
282  * @id: i2c client device id
283  *
284  * Return 0 on success, negative on failure.
285  *
286  * Register the i2c client device as a MOST interface
287  */
288 static int i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
289 {
290 	struct hdm_i2c *dev;
291 	int ret, i;
292 
293 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
294 	if (!dev)
295 		return -ENOMEM;
296 
297 	/* ID format: i2c-<bus>-<address> */
298 	snprintf(dev->name, sizeof(dev->name), "i2c-%d-%04x",
299 		 client->adapter->nr, client->addr);
300 
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;
305 	}
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";
310 
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;
318 
319 	INIT_LIST_HEAD(&dev->rx.list);
320 
321 	INIT_DELAYED_WORK(&dev->rx.dwork, pending_rx_work);
322 
323 	dev->client = client;
324 	i2c_set_clientdata(client, dev);
325 
326 	ret = most_register_interface(&dev->most_iface);
327 	if (ret) {
328 		pr_err("Failed to register i2c as a MOST interface\n");
329 		kfree(dev);
330 		return ret;
331 	}
332 
333 	return 0;
334 }
335 
336 /*
337  * i2c_remove - i2c remove handler
338  * @client: i2c client device structure
339  *
340  * Return 0 on success.
341  *
342  * Unregister the i2c client device as a MOST interface
343  */
344 static int i2c_remove(struct i2c_client *client)
345 {
346 	struct hdm_i2c *dev = i2c_get_clientdata(client);
347 
348 	most_deregister_interface(&dev->most_iface);
349 	kfree(dev);
350 
351 	return 0;
352 }
353 
354 static const struct i2c_device_id i2c_id[] = {
355 	{ "most_i2c", 0 },
356 	{ }, /* Terminating entry */
357 };
358 
359 MODULE_DEVICE_TABLE(i2c, i2c_id);
360 
361 static struct i2c_driver i2c_driver = {
362 	.driver = {
363 		.name = "hdm_i2c",
364 	},
365 	.probe = i2c_probe,
366 	.remove = i2c_remove,
367 	.id_table = i2c_id,
368 };
369 
370 module_i2c_driver(i2c_driver);
371 
372 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
373 MODULE_DESCRIPTION("I2C Hardware Dependent Module");
374 MODULE_LICENSE("GPL");
375