1
2
3
4
5
6
7
8
9
10
11
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
26enum { CH_RX, CH_TX, NUM_CHANNELS };
27
28#define MAX_BUFFERS_CONTROL 32
29#define MAX_BUF_SIZE_CONTROL 256
30
31
32
33
34
35#define list_first_mbo(ptr) \
36 list_first_entry(ptr, struct mbo, list)
37
38
39static bool polling_req;
40module_param(polling_req, bool, 0444);
41MODULE_PARM_DESC(polling_req, "Request Polling. Default = 0 (use irq)");
42
43
44static int scan_rate = 100;
45module_param(scan_rate, int, 0644);
46MODULE_PARM_DESC(scan_rate, "Polling rate in times/sec. Default = 100");
47
48struct hdm_i2c {
49 bool is_open[NUM_CHANNELS];
50 bool polling_mode;
51 struct most_interface most_iface;
52 struct most_channel_capability capabilities[NUM_CHANNELS];
53 struct i2c_client *client;
54 struct rx {
55 struct delayed_work dwork;
56 wait_queue_head_t waitq;
57 struct list_head list;
58 struct mutex list_mutex;
59 } rx;
60 char name[64];
61};
62
63#define to_hdm(iface) container_of(iface, struct hdm_i2c, most_iface)
64
65
66
67
68
69
70
71
72
73
74
75
76static int configure_channel(struct most_interface *most_iface,
77 int ch_idx,
78 struct most_channel_config *channel_config)
79{
80 struct hdm_i2c *dev = to_hdm(most_iface);
81
82 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
83 BUG_ON(dev->is_open[ch_idx]);
84
85 if (channel_config->data_type != MOST_CH_CONTROL) {
86 pr_err("bad data type for channel %d\n", ch_idx);
87 return -EPERM;
88 }
89
90 if (channel_config->direction != dev->capabilities[ch_idx].direction) {
91 pr_err("bad direction for channel %d\n", ch_idx);
92 return -EPERM;
93 }
94
95 if ((channel_config->direction == MOST_CH_RX) && (dev->polling_mode)) {
96 schedule_delayed_work(&dev->rx.dwork,
97 msecs_to_jiffies(MSEC_PER_SEC / 4));
98 }
99 dev->is_open[ch_idx] = true;
100
101 return 0;
102}
103
104
105
106
107
108
109
110
111
112
113
114
115static int enqueue(struct most_interface *most_iface,
116 int ch_idx, struct mbo *mbo)
117{
118 struct hdm_i2c *dev = to_hdm(most_iface);
119 int ret;
120
121 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
122 BUG_ON(!dev->is_open[ch_idx]);
123
124 if (ch_idx == CH_RX) {
125
126 mutex_lock(&dev->rx.list_mutex);
127 list_add_tail(&mbo->list, &dev->rx.list);
128 mutex_unlock(&dev->rx.list_mutex);
129 wake_up_interruptible(&dev->rx.waitq);
130 } else {
131
132 ret = i2c_master_send(dev->client, mbo->virt_address,
133 mbo->buffer_length);
134 if (ret <= 0) {
135 mbo->processed_length = 0;
136 mbo->status = MBO_E_INVAL;
137 } else {
138 mbo->processed_length = mbo->buffer_length;
139 mbo->status = MBO_SUCCESS;
140 }
141 mbo->complete(mbo);
142 }
143
144 return 0;
145}
146
147
148
149
150
151
152
153
154
155
156
157static int poison_channel(struct most_interface *most_iface,
158 int ch_idx)
159{
160 struct hdm_i2c *dev = to_hdm(most_iface);
161 struct mbo *mbo;
162
163 BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
164 BUG_ON(!dev->is_open[ch_idx]);
165
166 dev->is_open[ch_idx] = false;
167
168 if (ch_idx == CH_RX) {
169 mutex_lock(&dev->rx.list_mutex);
170 while (!list_empty(&dev->rx.list)) {
171 mbo = list_first_mbo(&dev->rx.list);
172 list_del(&mbo->list);
173 mutex_unlock(&dev->rx.list_mutex);
174
175 mbo->processed_length = 0;
176 mbo->status = MBO_E_CLOSE;
177 mbo->complete(mbo);
178
179 mutex_lock(&dev->rx.list_mutex);
180 }
181 mutex_unlock(&dev->rx.list_mutex);
182 wake_up_interruptible(&dev->rx.waitq);
183 }
184
185 return 0;
186}
187
188static void do_rx_work(struct hdm_i2c *dev)
189{
190 struct mbo *mbo;
191 unsigned char msg[MAX_BUF_SIZE_CONTROL];
192 int ret, ch_idx = CH_RX;
193 u16 pml, data_size;
194
195
196 ret = i2c_master_recv(dev->client, msg, 2);
197 if (ret <= 0) {
198 pr_err("Failed to receive PML\n");
199 return;
200 }
201
202 pml = (msg[0] << 8) | msg[1];
203 if (!pml)
204 return;
205
206 data_size = pml + 2;
207
208
209 ret = i2c_master_recv(dev->client, msg, data_size);
210 if (ret <= 0) {
211 pr_err("Failed to receive a Port Message\n");
212 return;
213 }
214
215 for (;;) {
216
217
218
219 if (wait_event_interruptible(dev->rx.waitq,
220 !dev->is_open[ch_idx] ||
221 !list_empty(&dev->rx.list))) {
222 pr_err("wait_event_interruptible() failed\n");
223 return;
224 }
225
226 if (!dev->is_open[ch_idx])
227 return;
228
229 mutex_lock(&dev->rx.list_mutex);
230
231
232 if (!list_empty(&dev->rx.list))
233 break;
234
235 mutex_unlock(&dev->rx.list_mutex);
236 }
237
238 mbo = list_first_mbo(&dev->rx.list);
239 list_del(&mbo->list);
240 mutex_unlock(&dev->rx.list_mutex);
241
242 mbo->processed_length = min(data_size, mbo->buffer_length);
243 memcpy(mbo->virt_address, msg, mbo->processed_length);
244 mbo->status = MBO_SUCCESS;
245 mbo->complete(mbo);
246}
247
248
249
250
251
252
253
254static void pending_rx_work(struct work_struct *work)
255{
256 struct hdm_i2c *dev = container_of(work, struct hdm_i2c, rx.dwork.work);
257
258 do_rx_work(dev);
259
260 if (dev->polling_mode) {
261 if (dev->is_open[CH_RX])
262 schedule_delayed_work(&dev->rx.dwork,
263 msecs_to_jiffies(MSEC_PER_SEC
264 / scan_rate));
265 } else {
266 enable_irq(dev->client->irq);
267 }
268}
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288static irqreturn_t most_irq_handler(int irq, void *_dev)
289{
290 struct hdm_i2c *dev = _dev;
291
292 disable_irq_nosync(irq);
293
294 schedule_delayed_work(&dev->rx.dwork, 0);
295
296 return IRQ_HANDLED;
297}
298
299
300
301
302
303
304
305
306
307
308static int i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
309{
310 struct hdm_i2c *dev;
311 int ret, i;
312 struct kobject *kobj;
313
314 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
315 if (!dev)
316 return -ENOMEM;
317
318
319 snprintf(dev->name, sizeof(dev->name), "i2c-%d-%04x",
320 client->adapter->nr, client->addr);
321
322 for (i = 0; i < NUM_CHANNELS; i++) {
323 dev->is_open[i] = false;
324 dev->capabilities[i].data_type = MOST_CH_CONTROL;
325 dev->capabilities[i].num_buffers_packet = MAX_BUFFERS_CONTROL;
326 dev->capabilities[i].buffer_size_packet = MAX_BUF_SIZE_CONTROL;
327 }
328 dev->capabilities[CH_RX].direction = MOST_CH_RX;
329 dev->capabilities[CH_RX].name_suffix = "rx";
330 dev->capabilities[CH_TX].direction = MOST_CH_TX;
331 dev->capabilities[CH_TX].name_suffix = "tx";
332
333 dev->most_iface.interface = ITYPE_I2C;
334 dev->most_iface.description = dev->name;
335 dev->most_iface.num_channels = NUM_CHANNELS;
336 dev->most_iface.channel_vector = dev->capabilities;
337 dev->most_iface.configure = configure_channel;
338 dev->most_iface.enqueue = enqueue;
339 dev->most_iface.poison_channel = poison_channel;
340
341 INIT_LIST_HEAD(&dev->rx.list);
342 mutex_init(&dev->rx.list_mutex);
343 init_waitqueue_head(&dev->rx.waitq);
344
345 INIT_DELAYED_WORK(&dev->rx.dwork, pending_rx_work);
346
347 dev->client = client;
348 i2c_set_clientdata(client, dev);
349
350 kobj = most_register_interface(&dev->most_iface);
351 if (IS_ERR(kobj)) {
352 pr_err("Failed to register i2c as a MOST interface\n");
353 kfree(dev);
354 return PTR_ERR(kobj);
355 }
356
357 dev->polling_mode = polling_req || client->irq <= 0;
358 if (!dev->polling_mode) {
359 pr_info("Requesting IRQ: %d\n", client->irq);
360 ret = request_irq(client->irq, most_irq_handler, 0,
361 client->name, dev);
362 if (ret) {
363 pr_info("IRQ request failed: %d, falling back to polling\n",
364 ret);
365 dev->polling_mode = true;
366 }
367 }
368
369 if (dev->polling_mode)
370 pr_info("Using polling at rate: %d times/sec\n", scan_rate);
371
372 return 0;
373}
374
375
376
377
378
379
380
381
382
383static int i2c_remove(struct i2c_client *client)
384{
385 struct hdm_i2c *dev = i2c_get_clientdata(client);
386 int i;
387
388 if (!dev->polling_mode)
389 free_irq(client->irq, dev);
390
391 most_deregister_interface(&dev->most_iface);
392
393 for (i = 0 ; i < NUM_CHANNELS; i++)
394 if (dev->is_open[i])
395 poison_channel(&dev->most_iface, i);
396 cancel_delayed_work_sync(&dev->rx.dwork);
397 kfree(dev);
398
399 return 0;
400}
401
402static const struct i2c_device_id i2c_id[] = {
403 { "most_i2c", 0 },
404 { },
405};
406
407MODULE_DEVICE_TABLE(i2c, i2c_id);
408
409static struct i2c_driver i2c_driver = {
410 .driver = {
411 .name = "hdm_i2c",
412 },
413 .probe = i2c_probe,
414 .remove = i2c_remove,
415 .id_table = i2c_id,
416};
417
418module_i2c_driver(i2c_driver);
419
420MODULE_AUTHOR("Jain Roy Ambi <JainRoy.Ambi@microchip.com>");
421MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
422MODULE_DESCRIPTION("I2C Hardware Dependent Module");
423MODULE_LICENSE("GPL");
424