Merge branch 'drm-tda9950-fixes' of git://git.armlinux.org.uk/~rmk/linux-arm into...
[muen/linux.git] / drivers / gpu / drm / i2c / tda9950.c
1 /*
2  *  TDA9950 Consumer Electronics Control driver
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * The NXP TDA9950 implements the HDMI Consumer Electronics Control
9  * interface.  The host interface is similar to a mailbox: the data
10  * registers starting at REG_CDR0 are written to send a command to the
11  * internal CPU, and replies are read from these registers.
12  *
13  * As the data registers represent a mailbox, they must be accessed
14  * as a single I2C transaction.  See the TDA9950 data sheet for details.
15  */
16 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/platform_data/tda9950.h>
21 #include <linux/slab.h>
22 #include <drm/drm_edid.h>
23 #include <media/cec.h>
24 #include <media/cec-notifier.h>
25
26 enum {
27         REG_CSR = 0x00,
28         CSR_BUSY = BIT(7),
29         CSR_INT  = BIT(6),
30         CSR_ERR  = BIT(5),
31
32         REG_CER = 0x01,
33
34         REG_CVR = 0x02,
35
36         REG_CCR = 0x03,
37         CCR_RESET = BIT(7),
38         CCR_ON    = BIT(6),
39
40         REG_ACKH = 0x04,
41         REG_ACKL = 0x05,
42
43         REG_CCONR = 0x06,
44         CCONR_ENABLE_ERROR = BIT(4),
45         CCONR_RETRY_MASK = 7,
46
47         REG_CDR0 = 0x07,
48
49         CDR1_REQ = 0x00,
50         CDR1_CNF = 0x01,
51         CDR1_IND = 0x81,
52         CDR1_ERR = 0x82,
53         CDR1_IER = 0x83,
54
55         CDR2_CNF_SUCCESS    = 0x00,
56         CDR2_CNF_OFF_STATE  = 0x80,
57         CDR2_CNF_BAD_REQ    = 0x81,
58         CDR2_CNF_CEC_ACCESS = 0x82,
59         CDR2_CNF_ARB_ERROR  = 0x83,
60         CDR2_CNF_BAD_TIMING = 0x84,
61         CDR2_CNF_NACK_ADDR  = 0x85,
62         CDR2_CNF_NACK_DATA  = 0x86,
63 };
64
65 struct tda9950_priv {
66         struct i2c_client *client;
67         struct device *hdmi;
68         struct cec_adapter *adap;
69         struct tda9950_glue *glue;
70         u16 addresses;
71         struct cec_msg rx_msg;
72         struct cec_notifier *notify;
73         bool open;
74 };
75
76 static int tda9950_write_range(struct i2c_client *client, u8 addr, u8 *p, int cnt)
77 {
78         struct i2c_msg msg;
79         u8 buf[CEC_MAX_MSG_SIZE + 3];
80         int ret;
81
82         if (WARN_ON(cnt > sizeof(buf) - 1))
83                 return -EINVAL;
84
85         buf[0] = addr;
86         memcpy(buf + 1, p, cnt);
87
88         msg.addr = client->addr;
89         msg.flags = 0;
90         msg.len = cnt + 1;
91         msg.buf = buf;
92
93         dev_dbg(&client->dev, "wr 0x%02x: %*ph\n", addr, cnt, p);
94
95         ret = i2c_transfer(client->adapter, &msg, 1);
96         if (ret < 0)
97                 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
98         return ret < 0 ? ret : 0;
99 }
100
101 static void tda9950_write(struct i2c_client *client, u8 addr, u8 val)
102 {
103         tda9950_write_range(client, addr, &val, 1);
104 }
105
106 static int tda9950_read_range(struct i2c_client *client, u8 addr, u8 *p, int cnt)
107 {
108         struct i2c_msg msg[2];
109         int ret;
110
111         msg[0].addr = client->addr;
112         msg[0].flags = 0;
113         msg[0].len = 1;
114         msg[0].buf = &addr;
115         msg[1].addr = client->addr;
116         msg[1].flags = I2C_M_RD;
117         msg[1].len = cnt;
118         msg[1].buf = p;
119
120         ret = i2c_transfer(client->adapter, msg, 2);
121         if (ret < 0)
122                 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
123
124         dev_dbg(&client->dev, "rd 0x%02x: %*ph\n", addr, cnt, p);
125
126         return ret;
127 }
128
129 static u8 tda9950_read(struct i2c_client *client, u8 addr)
130 {
131         int ret;
132         u8 val;
133
134         ret = tda9950_read_range(client, addr, &val, 1);
135         if (ret < 0)
136                 val = 0;
137
138         return val;
139 }
140
141 static irqreturn_t tda9950_irq(int irq, void *data)
142 {
143         struct tda9950_priv *priv = data;
144         unsigned int tx_status;
145         u8 csr, cconr, buf[19];
146         u8 arb_lost_cnt, nack_cnt, err_cnt;
147
148         if (!priv->open)
149                 return IRQ_NONE;
150
151         csr = tda9950_read(priv->client, REG_CSR);
152         if (!(csr & CSR_INT))
153                 return IRQ_NONE;
154
155         cconr = tda9950_read(priv->client, REG_CCONR) & CCONR_RETRY_MASK;
156
157         tda9950_read_range(priv->client, REG_CDR0, buf, sizeof(buf));
158
159         /*
160          * This should never happen: the data sheet says that there will
161          * always be a valid message if the interrupt line is asserted.
162          */
163         if (buf[0] == 0) {
164                 dev_warn(&priv->client->dev, "interrupt pending, but no message?\n");
165                 return IRQ_NONE;
166         }
167
168         switch (buf[1]) {
169         case CDR1_CNF: /* transmit result */
170                 arb_lost_cnt = nack_cnt = err_cnt = 0;
171                 switch (buf[2]) {
172                 case CDR2_CNF_SUCCESS:
173                         tx_status = CEC_TX_STATUS_OK;
174                         break;
175
176                 case CDR2_CNF_ARB_ERROR:
177                         tx_status = CEC_TX_STATUS_ARB_LOST;
178                         arb_lost_cnt = cconr;
179                         break;
180
181                 case CDR2_CNF_NACK_ADDR:
182                         tx_status = CEC_TX_STATUS_NACK;
183                         nack_cnt = cconr;
184                         break;
185
186                 default: /* some other error, refer to TDA9950 docs */
187                         dev_err(&priv->client->dev, "CNF reply error 0x%02x\n",
188                                 buf[2]);
189                         tx_status = CEC_TX_STATUS_ERROR;
190                         err_cnt = cconr;
191                         break;
192                 }
193                 /* TDA9950 executes all retries for us */
194                 if (tx_status != CEC_TX_STATUS_OK)
195                         tx_status |= CEC_TX_STATUS_MAX_RETRIES;
196                 cec_transmit_done(priv->adap, tx_status, arb_lost_cnt,
197                                   nack_cnt, 0, err_cnt);
198                 break;
199
200         case CDR1_IND:
201                 priv->rx_msg.len = buf[0] - 2;
202                 if (priv->rx_msg.len > CEC_MAX_MSG_SIZE)
203                         priv->rx_msg.len = CEC_MAX_MSG_SIZE;
204
205                 memcpy(priv->rx_msg.msg, buf + 2, priv->rx_msg.len);
206                 cec_received_msg(priv->adap, &priv->rx_msg);
207                 break;
208
209         default: /* unknown */
210                 dev_err(&priv->client->dev, "unknown service id 0x%02x\n",
211                         buf[1]);
212                 break;
213         }
214
215         return IRQ_HANDLED;
216 }
217
218 static int tda9950_cec_transmit(struct cec_adapter *adap, u8 attempts,
219                                 u32 signal_free_time, struct cec_msg *msg)
220 {
221         struct tda9950_priv *priv = adap->priv;
222         u8 buf[CEC_MAX_MSG_SIZE + 2];
223
224         buf[0] = 2 + msg->len;
225         buf[1] = CDR1_REQ;
226         memcpy(buf + 2, msg->msg, msg->len);
227
228         if (attempts > 5)
229                 attempts = 5;
230
231         tda9950_write(priv->client, REG_CCONR, attempts);
232
233         return tda9950_write_range(priv->client, REG_CDR0, buf, 2 + msg->len);
234 }
235
236 static int tda9950_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
237 {
238         struct tda9950_priv *priv = adap->priv;
239         u16 addresses;
240         u8 buf[2];
241
242         if (addr == CEC_LOG_ADDR_INVALID)
243                 addresses = priv->addresses = 0;
244         else
245                 addresses = priv->addresses |= BIT(addr);
246
247         /* TDA9950 doesn't want address 15 set */
248         addresses &= 0x7fff;
249         buf[0] = addresses >> 8;
250         buf[1] = addresses;
251
252         return tda9950_write_range(priv->client, REG_ACKH, buf, 2);
253 }
254
255 /*
256  * When operating as part of the TDA998x, we need additional handling
257  * to initialise and shut down the TDA9950 part of the device.  These
258  * two hooks are provided to allow the TDA998x code to perform those
259  * activities.
260  */
261 static int tda9950_glue_open(struct tda9950_priv *priv)
262 {
263         int ret = 0;
264
265         if (priv->glue && priv->glue->open)
266                 ret = priv->glue->open(priv->glue->data);
267
268         priv->open = true;
269
270         return ret;
271 }
272
273 static void tda9950_glue_release(struct tda9950_priv *priv)
274 {
275         priv->open = false;
276
277         if (priv->glue && priv->glue->release)
278                 priv->glue->release(priv->glue->data);
279 }
280
281 static int tda9950_open(struct tda9950_priv *priv)
282 {
283         struct i2c_client *client = priv->client;
284         int ret;
285
286         ret = tda9950_glue_open(priv);
287         if (ret)
288                 return ret;
289
290         /* Reset the TDA9950, and wait 250ms for it to recover */
291         tda9950_write(client, REG_CCR, CCR_RESET);
292         msleep(250);
293
294         tda9950_cec_adap_log_addr(priv->adap, CEC_LOG_ADDR_INVALID);
295
296         /* Start the command processor */
297         tda9950_write(client, REG_CCR, CCR_ON);
298
299         return 0;
300 }
301
302 static void tda9950_release(struct tda9950_priv *priv)
303 {
304         struct i2c_client *client = priv->client;
305         int timeout = 50;
306         u8 csr;
307
308         /* Stop the command processor */
309         tda9950_write(client, REG_CCR, 0);
310
311         /* Wait up to .5s for it to signal non-busy */
312         do {
313                 csr = tda9950_read(client, REG_CSR);
314                 if (!(csr & CSR_BUSY) || !--timeout)
315                         break;
316                 msleep(10);
317         } while (1);
318
319         /* Warn the user that their IRQ may die if it's shared. */
320         if (csr & CSR_BUSY)
321                 dev_warn(&client->dev, "command processor failed to stop, irq%d may die (csr=0x%02x)\n",
322                          client->irq, csr);
323
324         tda9950_glue_release(priv);
325 }
326
327 static int tda9950_cec_adap_enable(struct cec_adapter *adap, bool enable)
328 {
329         struct tda9950_priv *priv = adap->priv;
330
331         if (!enable) {
332                 tda9950_release(priv);
333                 return 0;
334         } else {
335                 return tda9950_open(priv);
336         }
337 }
338
339 static const struct cec_adap_ops tda9950_cec_ops = {
340         .adap_enable = tda9950_cec_adap_enable,
341         .adap_log_addr = tda9950_cec_adap_log_addr,
342         .adap_transmit = tda9950_cec_transmit,
343 };
344
345 /*
346  * When operating as part of the TDA998x, we need to claim additional
347  * resources.  These two hooks permit the management of those resources.
348  */
349 static void tda9950_devm_glue_exit(void *data)
350 {
351         struct tda9950_glue *glue = data;
352
353         if (glue && glue->exit)
354                 glue->exit(glue->data);
355 }
356
357 static int tda9950_devm_glue_init(struct device *dev, struct tda9950_glue *glue)
358 {
359         int ret;
360
361         if (glue && glue->init) {
362                 ret = glue->init(glue->data);
363                 if (ret)
364                         return ret;
365         }
366
367         ret = devm_add_action(dev, tda9950_devm_glue_exit, glue);
368         if (ret)
369                 tda9950_devm_glue_exit(glue);
370
371         return ret;
372 }
373
374 static void tda9950_cec_del(void *data)
375 {
376         struct tda9950_priv *priv = data;
377
378         cec_delete_adapter(priv->adap);
379 }
380
381 static int tda9950_probe(struct i2c_client *client,
382                          const struct i2c_device_id *id)
383 {
384         struct tda9950_glue *glue = client->dev.platform_data;
385         struct device *dev = &client->dev;
386         struct tda9950_priv *priv;
387         unsigned long irqflags;
388         int ret;
389         u8 cvr;
390
391         /*
392          * We must have I2C functionality: our multi-byte accesses
393          * must be performed as a single contiguous transaction.
394          */
395         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
396                 dev_err(&client->dev,
397                         "adapter does not support I2C functionality\n");
398                 return -ENXIO;
399         }
400
401         /* We must have an interrupt to be functional. */
402         if (client->irq <= 0) {
403                 dev_err(&client->dev, "driver requires an interrupt\n");
404                 return -ENXIO;
405         }
406
407         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
408         if (!priv)
409                 return -ENOMEM;
410
411         priv->client = client;
412         priv->glue = glue;
413
414         i2c_set_clientdata(client, priv);
415
416         /*
417          * If we're part of a TDA998x, we want the class devices to be
418          * associated with the HDMI Tx so we have a tight relationship
419          * between the HDMI interface and the CEC interface.
420          */
421         priv->hdmi = dev;
422         if (glue && glue->parent)
423                 priv->hdmi = glue->parent;
424
425         priv->adap = cec_allocate_adapter(&tda9950_cec_ops, priv, "tda9950",
426                                           CEC_CAP_DEFAULTS,
427                                           CEC_MAX_LOG_ADDRS);
428         if (IS_ERR(priv->adap))
429                 return PTR_ERR(priv->adap);
430
431         ret = devm_add_action(dev, tda9950_cec_del, priv);
432         if (ret) {
433                 cec_delete_adapter(priv->adap);
434                 return ret;
435         }
436
437         ret = tda9950_devm_glue_init(dev, glue);
438         if (ret)
439                 return ret;
440
441         ret = tda9950_glue_open(priv);
442         if (ret)
443                 return ret;
444
445         cvr = tda9950_read(client, REG_CVR);
446
447         dev_info(&client->dev,
448                  "TDA9950 CEC interface, hardware version %u.%u\n",
449                  cvr >> 4, cvr & 15);
450
451         tda9950_glue_release(priv);
452
453         irqflags = IRQF_TRIGGER_FALLING;
454         if (glue)
455                 irqflags = glue->irq_flags;
456
457         ret = devm_request_threaded_irq(dev, client->irq, NULL, tda9950_irq,
458                                         irqflags | IRQF_SHARED | IRQF_ONESHOT,
459                                         dev_name(&client->dev), priv);
460         if (ret < 0)
461                 return ret;
462
463         priv->notify = cec_notifier_get(priv->hdmi);
464         if (!priv->notify)
465                 return -ENOMEM;
466
467         ret = cec_register_adapter(priv->adap, priv->hdmi);
468         if (ret < 0) {
469                 cec_notifier_put(priv->notify);
470                 return ret;
471         }
472
473         /*
474          * CEC documentation says we must not call cec_delete_adapter
475          * after a successful call to cec_register_adapter().
476          */
477         devm_remove_action(dev, tda9950_cec_del, priv);
478
479         cec_register_cec_notifier(priv->adap, priv->notify);
480
481         return 0;
482 }
483
484 static int tda9950_remove(struct i2c_client *client)
485 {
486         struct tda9950_priv *priv = i2c_get_clientdata(client);
487
488         cec_unregister_adapter(priv->adap);
489         cec_notifier_put(priv->notify);
490
491         return 0;
492 }
493
494 static struct i2c_device_id tda9950_ids[] = {
495         { "tda9950", 0 },
496         { },
497 };
498 MODULE_DEVICE_TABLE(i2c, tda9950_ids);
499
500 static struct i2c_driver tda9950_driver = {
501         .probe = tda9950_probe,
502         .remove = tda9950_remove,
503         .driver = {
504                 .name = "tda9950",
505         },
506         .id_table = tda9950_ids,
507 };
508
509 module_i2c_driver(tda9950_driver);
510
511 MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
512 MODULE_DESCRIPTION("TDA9950/TDA998x Consumer Electronics Control Driver");
513 MODULE_LICENSE("GPL v2");