1
2
3
4
5
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/core.h"
18
19enum { CH_RX, CH_TX, NUM_CHANNELS };
20
21#define MAX_BUFFERS_CONTROL 32
22#define MAX_BUF_SIZE_CONTROL 256
23
24
25
26
27
28#define list_first_mbo(ptr) \
29 list_first_entry(ptr, struct mbo, list)
30
31static unsigned int polling_rate;
32module_param(polling_rate, uint, 0644);
33MODULE_PARM_DESC(polling_rate, "Polling rate [Hz]. Default = 0 (use IRQ)");
34
35struct 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
50static irqreturn_t most_irq_handler(int, void *);
51static void pending_rx_work(struct work_struct *);
52
53
54
55
56
57
58
59
60
61
62
63
64static 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
111
112
113
114
115
116
117
118
119
120static 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
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
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
157
158
159
160
161
162
163
164
165static 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
191static 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
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
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
229
230
231
232
233static 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268static 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
281
282
283
284
285
286
287
288static 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
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
338
339
340
341
342
343
344static 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
354static const struct i2c_device_id i2c_id[] = {
355 { "most_i2c", 0 },
356 { },
357};
358
359MODULE_DEVICE_TABLE(i2c, i2c_id);
360
361static 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
370module_i2c_driver(i2c_driver);
371
372MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
373MODULE_DESCRIPTION("I2C Hardware Dependent Module");
374MODULE_LICENSE("GPL");
375