Merge remote-tracking branch 'airlied/drm-next' into drm-intel-next
[firefly-linux-kernel-4.4.55.git] / drivers / staging / most / hdm-i2c / hdm_i2c.c
1 /*
2  * hdm_i2c.c - Hardware Dependent Module for I2C Interface
3  *
4  * Copyright (C) 2013-2015, Microchip Technology Germany II GmbH & Co. KG
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * This file is licensed under GPLv2.
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/sched.h>
21 #include <linux/interrupt.h>
22 #include <linux/err.h>
23
24 #include <mostcore.h>
25
26 enum { CH_RX, CH_TX, NUM_CHANNELS };
27
28 #define MAX_BUFFERS_CONTROL 32
29 #define MAX_BUF_SIZE_CONTROL 256
30
31 /**
32  * list_first_mbo - get the first mbo from a list
33  * @ptr:        the list head to take the mbo from.
34  */
35 #define list_first_mbo(ptr) \
36         list_first_entry(ptr, struct mbo, list)
37
38
39 /* IRQ / Polling option */
40 static bool polling_req;
41 module_param(polling_req, bool, S_IRUGO);
42 MODULE_PARM_DESC(polling_req, "Request Polling. Default = 0 (use irq)");
43
44 /* Polling Rate */
45 static int scan_rate = 100;
46 module_param(scan_rate, int, 0644);
47 MODULE_PARM_DESC(scan_rate, "Polling rate in times/sec. Default = 100");
48
49 struct hdm_i2c {
50         bool is_open[NUM_CHANNELS];
51         bool polling_mode;
52         struct most_interface most_iface;
53         struct most_channel_capability capabilities[NUM_CHANNELS];
54         struct i2c_client *client;
55         struct rx {
56                 struct delayed_work dwork;
57                 wait_queue_head_t waitq;
58                 struct list_head list;
59                 struct mutex list_mutex;
60         } rx;
61         char name[64];
62 };
63
64 #define to_hdm(iface) container_of(iface, struct hdm_i2c, most_iface)
65
66 /**
67  * configure_channel - called from MOST core to configure a channel
68  * @iface: interface the channel belongs to
69  * @channel: channel to be configured
70  * @channel_config: structure that holds the configuration information
71  *
72  * Return 0 on success, negative on failure.
73  *
74  * Receives configuration information from MOST core and initialize the
75  * corresponding channel.
76  */
77 static int configure_channel(struct most_interface *most_iface,
78                              int ch_idx,
79                              struct most_channel_config *channel_config)
80 {
81         struct hdm_i2c *dev = to_hdm(most_iface);
82
83         BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
84         BUG_ON(dev->is_open[ch_idx]);
85
86         if (channel_config->data_type != MOST_CH_CONTROL) {
87                 pr_err("bad data type for channel %d\n", ch_idx);
88                 return -EPERM;
89         }
90
91         if (channel_config->direction != dev->capabilities[ch_idx].direction) {
92                 pr_err("bad direction for channel %d\n", ch_idx);
93                 return -EPERM;
94         }
95
96         if (channel_config->direction == MOST_CH_RX) {
97                 if (dev->polling_mode)
98                         schedule_delayed_work(&dev->rx.dwork,
99                                               msecs_to_jiffies(MSEC_PER_SEC / 4));
100         }
101         dev->is_open[ch_idx] = true;
102
103         return 0;
104 }
105
106 /**
107  * enqueue - called from MOST core to enqueue a buffer for data transfer
108  * @iface: intended interface
109  * @channel: ID of the channel the buffer is intended for
110  * @mbo: pointer to the buffer object
111  *
112  * Return 0 on success, negative on failure.
113  *
114  * Transmit the data over I2C if it is a "write" request or push the buffer into
115  * list if it is an "read" request
116  */
117 static int enqueue(struct most_interface *most_iface,
118                    int ch_idx, struct mbo *mbo)
119 {
120         struct hdm_i2c *dev = to_hdm(most_iface);
121         int ret;
122
123         BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
124         BUG_ON(!dev->is_open[ch_idx]);
125
126         if (ch_idx == CH_RX) {
127                 /* RX */
128                 mutex_lock(&dev->rx.list_mutex);
129                 list_add_tail(&mbo->list, &dev->rx.list);
130                 mutex_unlock(&dev->rx.list_mutex);
131                 wake_up_interruptible(&dev->rx.waitq);
132         } else {
133                 /* TX */
134                 ret = i2c_master_send(dev->client, mbo->virt_address,
135                                       mbo->buffer_length);
136                 if (ret <= 0) {
137                         mbo->processed_length = 0;
138                         mbo->status = MBO_E_INVAL;
139                 } else {
140                         mbo->processed_length = mbo->buffer_length;
141                         mbo->status = MBO_SUCCESS;
142                 }
143                 mbo->complete(mbo);
144         }
145
146         return 0;
147 }
148
149 /**
150  * poison_channel - called from MOST core to poison buffers of a channel
151  * @iface: pointer to the interface the channel to be poisoned belongs to
152  * @channel_id: corresponding channel ID
153  *
154  * Return 0 on success, negative on failure.
155  *
156  * If channel direction is RX, complete the buffers in list with
157  * status MBO_E_CLOSE
158  */
159 static int poison_channel(struct most_interface *most_iface,
160                           int ch_idx)
161 {
162         struct hdm_i2c *dev = to_hdm(most_iface);
163         struct mbo *mbo;
164
165         BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
166         BUG_ON(!dev->is_open[ch_idx]);
167
168         dev->is_open[ch_idx] = false;
169
170         if (ch_idx == CH_RX) {
171                 mutex_lock(&dev->rx.list_mutex);
172                 while (!list_empty(&dev->rx.list)) {
173                         mbo = list_first_mbo(&dev->rx.list);
174                         list_del(&mbo->list);
175                         mutex_unlock(&dev->rx.list_mutex);
176
177                         mbo->processed_length = 0;
178                         mbo->status = MBO_E_CLOSE;
179                         mbo->complete(mbo);
180
181                         mutex_lock(&dev->rx.list_mutex);
182                 }
183                 mutex_unlock(&dev->rx.list_mutex);
184                 wake_up_interruptible(&dev->rx.waitq);
185         }
186
187         return 0;
188 }
189
190 static void request_netinfo(struct most_interface *most_iface,
191                             int ch_idx)
192 {
193         pr_info("request_netinfo()\n");
194 }
195
196 static void do_rx_work(struct hdm_i2c *dev)
197 {
198         struct mbo *mbo;
199         unsigned char msg[MAX_BUF_SIZE_CONTROL];
200         int ret, ch_idx = CH_RX;
201         uint16_t pml, data_size;
202
203         /* Read PML (2 bytes) */
204         ret = i2c_master_recv(dev->client, msg, 2);
205         if (ret <= 0) {
206                 pr_err("Failed to receive PML\n");
207                 return;
208         }
209
210         pml = (msg[0] << 8) | msg[1];
211         if (!pml)
212                 return;
213
214         data_size = pml + 2;
215
216         /* Read the whole message, including PML */
217         ret = i2c_master_recv(dev->client, msg, data_size);
218         if (ret <= 0) {
219                 pr_err("Failed to receive a Port Message\n");
220                 return;
221         }
222
223         for (;;) {
224                 /* Conditions to wait for: poisoned channel or free buffer
225                    available for reading  */
226                 if (wait_event_interruptible(dev->rx.waitq,
227                                              !dev->is_open[ch_idx] ||
228                                              !list_empty(&dev->rx.list))) {
229                         pr_err("wait_event_interruptible() failed\n");
230                         return;
231                 }
232
233                 if (!dev->is_open[ch_idx])
234                         return;
235
236                 mutex_lock(&dev->rx.list_mutex);
237
238                 /* list may be empty if poison or remove is called */
239                 if (!list_empty(&dev->rx.list))
240                         break;
241
242                 mutex_unlock(&dev->rx.list_mutex);
243         }
244
245         mbo = list_first_mbo(&dev->rx.list);
246         list_del(&mbo->list);
247         mutex_unlock(&dev->rx.list_mutex);
248
249         mbo->processed_length = min(data_size, mbo->buffer_length);
250         memcpy(mbo->virt_address, msg, mbo->processed_length);
251         mbo->status = MBO_SUCCESS;
252         mbo->complete(mbo);
253 }
254
255 /**
256  * pending_rx_work - Read pending messages through I2C
257  * @work: definition of this work item
258  *
259  * Invoked by the Interrupt Service Routine, most_irq_handler()
260  */
261 static void pending_rx_work(struct work_struct *work)
262 {
263         struct hdm_i2c *dev = container_of(work, struct hdm_i2c, rx.dwork.work);
264
265         do_rx_work(dev);
266
267         if (dev->polling_mode) {
268                 if (dev->is_open[CH_RX])
269                         schedule_delayed_work(&dev->rx.dwork,
270                                               msecs_to_jiffies(MSEC_PER_SEC
271                                                                / scan_rate));
272         } else
273                 enable_irq(dev->client->irq);
274 }
275
276 /*
277  * most_irq_handler - Interrupt Service Routine
278  * @irq: irq number
279  * @_dev: private data
280  *
281  * Schedules a delayed work
282  *
283  * By default the interrupt line behavior is Active Low. Once an interrupt is
284  * generated by the device, until driver clears the interrupt (by reading
285  * the PMP message), device keeps the interrupt line in low state. Since i2c
286  * read is done in work queue, the interrupt line must be disabled temporarily
287  * to avoid ISR being called repeatedly. Re-enable the interrupt in workqueue,
288  * after reading the message.
289  *
290  * Note: If we use the interrupt line in Falling edge mode, there is a
291  * possibility to miss interrupts when ISR is getting executed.
292  *
293  */
294 static irqreturn_t most_irq_handler(int irq, void *_dev)
295 {
296         struct hdm_i2c *dev = _dev;
297
298         disable_irq_nosync(irq);
299
300         schedule_delayed_work(&dev->rx.dwork, 0);
301
302         return IRQ_HANDLED;
303 }
304
305 /*
306  * i2c_probe - i2c probe handler
307  * @client: i2c client device structure
308  * @id: i2c client device id
309  *
310  * Return 0 on success, negative on failure.
311  *
312  * Register the i2c client device as a MOST interface
313  */
314 static int i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
315 {
316         struct hdm_i2c *dev;
317         int ret, i;
318         struct kobject *kobj;
319
320         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
321         if (!dev)
322                 return -ENOMEM;
323
324         /* ID format: i2c-<bus>-<address> */
325         snprintf(dev->name, sizeof(dev->name), "i2c-%d-%04x",
326                  client->adapter->nr, client->addr);
327
328         for (i = 0; i < NUM_CHANNELS; i++) {
329                 dev->is_open[i] = false;
330                 dev->capabilities[i].data_type = MOST_CH_CONTROL;
331                 dev->capabilities[i].num_buffers_packet = MAX_BUFFERS_CONTROL;
332                 dev->capabilities[i].buffer_size_packet = MAX_BUF_SIZE_CONTROL;
333         }
334         dev->capabilities[CH_RX].direction = MOST_CH_RX;
335         dev->capabilities[CH_RX].name_suffix = "rx";
336         dev->capabilities[CH_TX].direction = MOST_CH_TX;
337         dev->capabilities[CH_TX].name_suffix = "tx";
338
339         dev->most_iface.interface = ITYPE_I2C;
340         dev->most_iface.description = dev->name;
341         dev->most_iface.num_channels = NUM_CHANNELS;
342         dev->most_iface.channel_vector = dev->capabilities;
343         dev->most_iface.configure = configure_channel;
344         dev->most_iface.enqueue = enqueue;
345         dev->most_iface.poison_channel = poison_channel;
346         dev->most_iface.request_netinfo = request_netinfo;
347
348         INIT_LIST_HEAD(&dev->rx.list);
349         mutex_init(&dev->rx.list_mutex);
350         init_waitqueue_head(&dev->rx.waitq);
351
352         INIT_DELAYED_WORK(&dev->rx.dwork, pending_rx_work);
353
354         dev->client = client;
355         i2c_set_clientdata(client, dev);
356
357         kobj = most_register_interface(&dev->most_iface);
358         if (IS_ERR(kobj)) {
359                 pr_err("Failed to register i2c as a MOST interface\n");
360                 kfree(dev);
361                 return PTR_ERR(kobj);
362         }
363
364         dev->polling_mode = polling_req || client->irq <= 0;
365         if (!dev->polling_mode) {
366                 pr_info("Requesting IRQ: %d\n", client->irq);
367                 ret = request_irq(client->irq, most_irq_handler, IRQF_SHARED,
368                                   client->name, dev);
369                 if (ret) {
370                         pr_info("IRQ request failed: %d, "
371                                 "falling back to polling\n", ret);
372                         dev->polling_mode = true;
373                 }
374         }
375
376         if (dev->polling_mode)
377                 pr_info("Using polling at rate: %d times/sec\n", scan_rate);
378
379         return 0;
380 }
381
382 /*
383  * i2c_remove - i2c remove handler
384  * @client: i2c client device structure
385  *
386  * Return 0 on success.
387  *
388  * Unregister the i2c client device as a MOST interface
389  */
390 static int i2c_remove(struct i2c_client *client)
391 {
392         struct hdm_i2c *dev = i2c_get_clientdata(client);
393         int i;
394
395         if (!dev->polling_mode)
396                 free_irq(client->irq, dev);
397
398         most_deregister_interface(&dev->most_iface);
399
400         for (i = 0 ; i < NUM_CHANNELS; i++)
401                 if (dev->is_open[i])
402                         poison_channel(&dev->most_iface, i);
403         cancel_delayed_work_sync(&dev->rx.dwork);
404         kfree(dev);
405
406         return 0;
407 }
408
409 static const struct i2c_device_id i2c_id[] = {
410         { "most_i2c", 0 },
411         { }, /* Terminating entry */
412 };
413
414 MODULE_DEVICE_TABLE(i2c, i2c_id);
415
416 static struct i2c_driver i2c_driver = {
417         .driver = {
418                 .name = "hdm_i2c",
419                 .owner = THIS_MODULE,
420         },
421         .probe = i2c_probe,
422         .remove = i2c_remove,
423         .id_table = i2c_id,
424 };
425
426 /**
427  * hdm_i2c_init - Driver Registration Routine
428  */
429 static int __init hdm_i2c_init(void)
430 {
431         pr_info("hdm_i2c_init()\n");
432
433         return i2c_add_driver(&i2c_driver);
434 }
435
436 /**
437  * hdm_i2c_exit - Driver Cleanup Routine
438  **/
439 static void __exit hdm_i2c_exit(void)
440 {
441         i2c_del_driver(&i2c_driver);
442         pr_info("hdm_i2c_exit()\n");
443 }
444
445 module_init(hdm_i2c_init);
446 module_exit(hdm_i2c_exit);
447
448 MODULE_AUTHOR("Jain Roy Ambi <JainRoy.Ambi@microchip.com>");
449 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
450 MODULE_DESCRIPTION("I2C Hardware Dependent Module");
451 MODULE_LICENSE("GPL");