xref: /openbmc/linux/drivers/staging/most/i2c/i2c.c (revision 160b8e75)
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/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/err.h>
17 
18 #include "most/core.h"
19 
20 enum { CH_RX, CH_TX, NUM_CHANNELS };
21 
22 #define MAX_BUFFERS_CONTROL 32
23 #define MAX_BUF_SIZE_CONTROL 256
24 
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 
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)");
36 
37 /* Polling Rate */
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");
41 
42 struct hdm_i2c {
43 	bool is_open[NUM_CHANNELS];
44 	bool polling_mode;
45 	struct most_interface most_iface;
46 	struct most_channel_capability capabilities[NUM_CHANNELS];
47 	struct i2c_client *client;
48 	struct rx {
49 		struct delayed_work dwork;
50 		wait_queue_head_t waitq;
51 		struct list_head list;
52 		struct mutex list_mutex;
53 	} rx;
54 	char name[64];
55 };
56 
57 #define to_hdm(iface) container_of(iface, struct hdm_i2c, most_iface)
58 
59 /**
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
64  *
65  * Return 0 on success, negative on failure.
66  *
67  * Receives configuration information from MOST core and initialize the
68  * corresponding channel.
69  */
70 static int configure_channel(struct most_interface *most_iface,
71 			     int ch_idx,
72 			     struct most_channel_config *channel_config)
73 {
74 	struct hdm_i2c *dev = to_hdm(most_iface);
75 
76 	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
77 	BUG_ON(dev->is_open[ch_idx]);
78 
79 	if (channel_config->data_type != MOST_CH_CONTROL) {
80 		pr_err("bad data type for channel %d\n", ch_idx);
81 		return -EPERM;
82 	}
83 
84 	if (channel_config->direction != dev->capabilities[ch_idx].direction) {
85 		pr_err("bad direction for channel %d\n", ch_idx);
86 		return -EPERM;
87 	}
88 
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));
92 	}
93 	dev->is_open[ch_idx] = true;
94 
95 	return 0;
96 }
97 
98 /**
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
103  *
104  * Return 0 on success, negative on failure.
105  *
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
108  */
109 static int enqueue(struct most_interface *most_iface,
110 		   int ch_idx, struct mbo *mbo)
111 {
112 	struct hdm_i2c *dev = to_hdm(most_iface);
113 	int ret;
114 
115 	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
116 	BUG_ON(!dev->is_open[ch_idx]);
117 
118 	if (ch_idx == CH_RX) {
119 		/* 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);
124 	} else {
125 		/* TX */
126 		ret = i2c_master_send(dev->client, mbo->virt_address,
127 				      mbo->buffer_length);
128 		if (ret <= 0) {
129 			mbo->processed_length = 0;
130 			mbo->status = MBO_E_INVAL;
131 		} else {
132 			mbo->processed_length = mbo->buffer_length;
133 			mbo->status = MBO_SUCCESS;
134 		}
135 		mbo->complete(mbo);
136 	}
137 
138 	return 0;
139 }
140 
141 /**
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
145  *
146  * Return 0 on success, negative on failure.
147  *
148  * If channel direction is RX, complete the buffers in list with
149  * status MBO_E_CLOSE
150  */
151 static int poison_channel(struct most_interface *most_iface,
152 			  int ch_idx)
153 {
154 	struct hdm_i2c *dev = to_hdm(most_iface);
155 	struct mbo *mbo;
156 
157 	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
158 	BUG_ON(!dev->is_open[ch_idx]);
159 
160 	dev->is_open[ch_idx] = false;
161 
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);
168 
169 			mbo->processed_length = 0;
170 			mbo->status = MBO_E_CLOSE;
171 			mbo->complete(mbo);
172 
173 			mutex_lock(&dev->rx.list_mutex);
174 		}
175 		mutex_unlock(&dev->rx.list_mutex);
176 		wake_up_interruptible(&dev->rx.waitq);
177 	}
178 
179 	return 0;
180 }
181 
182 static void do_rx_work(struct hdm_i2c *dev)
183 {
184 	struct mbo *mbo;
185 	unsigned char msg[MAX_BUF_SIZE_CONTROL];
186 	int ret, ch_idx = CH_RX;
187 	u16 pml, data_size;
188 
189 	/* Read PML (2 bytes) */
190 	ret = i2c_master_recv(dev->client, msg, 2);
191 	if (ret <= 0) {
192 		pr_err("Failed to receive PML\n");
193 		return;
194 	}
195 
196 	pml = (msg[0] << 8) | msg[1];
197 	if (!pml)
198 		return;
199 
200 	data_size = pml + 2;
201 
202 	/* Read the whole message, including PML */
203 	ret = i2c_master_recv(dev->client, msg, data_size);
204 	if (ret <= 0) {
205 		pr_err("Failed to receive a Port Message\n");
206 		return;
207 	}
208 
209 	for (;;) {
210 		/* Conditions to wait for: poisoned channel or free buffer
211 		 * available for reading
212 		 */
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");
217 			return;
218 		}
219 
220 		if (!dev->is_open[ch_idx])
221 			return;
222 
223 		mutex_lock(&dev->rx.list_mutex);
224 
225 		/* list may be empty if poison or remove is called */
226 		if (!list_empty(&dev->rx.list))
227 			break;
228 
229 		mutex_unlock(&dev->rx.list_mutex);
230 	}
231 
232 	mbo = list_first_mbo(&dev->rx.list);
233 	list_del(&mbo->list);
234 	mutex_unlock(&dev->rx.list_mutex);
235 
236 	mbo->processed_length = min(data_size, mbo->buffer_length);
237 	memcpy(mbo->virt_address, msg, mbo->processed_length);
238 	mbo->status = MBO_SUCCESS;
239 	mbo->complete(mbo);
240 }
241 
242 /**
243  * pending_rx_work - Read pending messages through I2C
244  * @work: definition of this work item
245  *
246  * Invoked by the Interrupt Service Routine, most_irq_handler()
247  */
248 static void pending_rx_work(struct work_struct *work)
249 {
250 	struct hdm_i2c *dev = container_of(work, struct hdm_i2c, rx.dwork.work);
251 
252 	do_rx_work(dev);
253 
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
258 							       / scan_rate));
259 	} else {
260 		enable_irq(dev->client->irq);
261 	}
262 }
263 
264 /*
265  * most_irq_handler - Interrupt Service Routine
266  * @irq: irq number
267  * @_dev: private data
268  *
269  * Schedules a delayed work
270  *
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.
277  *
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.
280  *
281  */
282 static irqreturn_t most_irq_handler(int irq, void *_dev)
283 {
284 	struct hdm_i2c *dev = _dev;
285 
286 	disable_irq_nosync(irq);
287 
288 	schedule_delayed_work(&dev->rx.dwork, 0);
289 
290 	return IRQ_HANDLED;
291 }
292 
293 /*
294  * i2c_probe - i2c probe handler
295  * @client: i2c client device structure
296  * @id: i2c client device id
297  *
298  * Return 0 on success, negative on failure.
299  *
300  * Register the i2c client device as a MOST interface
301  */
302 static int i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
303 {
304 	struct hdm_i2c *dev;
305 	int ret, i;
306 
307 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
308 	if (!dev)
309 		return -ENOMEM;
310 
311 	/* ID format: i2c-<bus>-<address> */
312 	snprintf(dev->name, sizeof(dev->name), "i2c-%d-%04x",
313 		 client->adapter->nr, client->addr);
314 
315 	for (i = 0; i < NUM_CHANNELS; i++) {
316 		dev->is_open[i] = false;
317 		dev->capabilities[i].data_type = MOST_CH_CONTROL;
318 		dev->capabilities[i].num_buffers_packet = MAX_BUFFERS_CONTROL;
319 		dev->capabilities[i].buffer_size_packet = MAX_BUF_SIZE_CONTROL;
320 	}
321 	dev->capabilities[CH_RX].direction = MOST_CH_RX;
322 	dev->capabilities[CH_RX].name_suffix = "rx";
323 	dev->capabilities[CH_TX].direction = MOST_CH_TX;
324 	dev->capabilities[CH_TX].name_suffix = "tx";
325 
326 	dev->most_iface.interface = ITYPE_I2C;
327 	dev->most_iface.description = dev->name;
328 	dev->most_iface.num_channels = NUM_CHANNELS;
329 	dev->most_iface.channel_vector = dev->capabilities;
330 	dev->most_iface.configure = configure_channel;
331 	dev->most_iface.enqueue = enqueue;
332 	dev->most_iface.poison_channel = poison_channel;
333 
334 	INIT_LIST_HEAD(&dev->rx.list);
335 	mutex_init(&dev->rx.list_mutex);
336 	init_waitqueue_head(&dev->rx.waitq);
337 
338 	INIT_DELAYED_WORK(&dev->rx.dwork, pending_rx_work);
339 
340 	dev->client = client;
341 	i2c_set_clientdata(client, dev);
342 
343 	ret = most_register_interface(&dev->most_iface);
344 	if (ret) {
345 		pr_err("Failed to register i2c as a MOST interface\n");
346 		kfree(dev);
347 		return ret;
348 	}
349 
350 	dev->polling_mode = polling_req || client->irq <= 0;
351 	if (!dev->polling_mode) {
352 		pr_info("Requesting IRQ: %d\n", client->irq);
353 		ret = request_irq(client->irq, most_irq_handler, 0,
354 				  client->name, dev);
355 		if (ret) {
356 			pr_info("IRQ request failed: %d, falling back to polling\n",
357 				ret);
358 			dev->polling_mode = true;
359 		}
360 	}
361 
362 	if (dev->polling_mode)
363 		pr_info("Using polling at rate: %d times/sec\n", scan_rate);
364 
365 	return 0;
366 }
367 
368 /*
369  * i2c_remove - i2c remove handler
370  * @client: i2c client device structure
371  *
372  * Return 0 on success.
373  *
374  * Unregister the i2c client device as a MOST interface
375  */
376 static int i2c_remove(struct i2c_client *client)
377 {
378 	struct hdm_i2c *dev = i2c_get_clientdata(client);
379 	int i;
380 
381 	if (!dev->polling_mode)
382 		free_irq(client->irq, dev);
383 
384 	most_deregister_interface(&dev->most_iface);
385 
386 	for (i = 0 ; i < NUM_CHANNELS; i++)
387 		if (dev->is_open[i])
388 			poison_channel(&dev->most_iface, i);
389 	cancel_delayed_work_sync(&dev->rx.dwork);
390 	kfree(dev);
391 
392 	return 0;
393 }
394 
395 static const struct i2c_device_id i2c_id[] = {
396 	{ "most_i2c", 0 },
397 	{ }, /* Terminating entry */
398 };
399 
400 MODULE_DEVICE_TABLE(i2c, i2c_id);
401 
402 static struct i2c_driver i2c_driver = {
403 	.driver = {
404 		.name = "hdm_i2c",
405 	},
406 	.probe = i2c_probe,
407 	.remove = i2c_remove,
408 	.id_table = i2c_id,
409 };
410 
411 module_i2c_driver(i2c_driver);
412 
413 MODULE_AUTHOR("Jain Roy Ambi <JainRoy.Ambi@microchip.com>");
414 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
415 MODULE_DESCRIPTION("I2C Hardware Dependent Module");
416 MODULE_LICENSE("GPL");
417