xref: /linux/drivers/media/cec/platform/sti/stih-cec.c (revision 495ae16a2895d6475384bca59f5128c9964dee0c)
14be5e864SMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0
24be5e864SMauro Carvalho Chehab /*
34be5e864SMauro Carvalho Chehab  * STIH4xx CEC driver
44be5e864SMauro Carvalho Chehab  * Copyright (C) STMicroelectronics SA 2016
54be5e864SMauro Carvalho Chehab  *
64be5e864SMauro Carvalho Chehab  */
74be5e864SMauro Carvalho Chehab #include <linux/clk.h>
84be5e864SMauro Carvalho Chehab #include <linux/interrupt.h>
9*495ae16aSAndy Shevchenko #include <linux/io.h>
104be5e864SMauro Carvalho Chehab #include <linux/kernel.h>
114be5e864SMauro Carvalho Chehab #include <linux/mfd/syscon.h>
124be5e864SMauro Carvalho Chehab #include <linux/module.h>
134be5e864SMauro Carvalho Chehab #include <linux/of.h>
144be5e864SMauro Carvalho Chehab #include <linux/of_platform.h>
154be5e864SMauro Carvalho Chehab #include <linux/platform_device.h>
164be5e864SMauro Carvalho Chehab 
174be5e864SMauro Carvalho Chehab #include <media/cec.h>
184be5e864SMauro Carvalho Chehab #include <media/cec-notifier.h>
194be5e864SMauro Carvalho Chehab 
204be5e864SMauro Carvalho Chehab #define CEC_NAME	"stih-cec"
214be5e864SMauro Carvalho Chehab 
224be5e864SMauro Carvalho Chehab /* CEC registers  */
234be5e864SMauro Carvalho Chehab #define CEC_CLK_DIV           0x0
244be5e864SMauro Carvalho Chehab #define CEC_CTRL              0x4
254be5e864SMauro Carvalho Chehab #define CEC_IRQ_CTRL          0x8
264be5e864SMauro Carvalho Chehab #define CEC_STATUS            0xC
274be5e864SMauro Carvalho Chehab #define CEC_EXT_STATUS        0x10
284be5e864SMauro Carvalho Chehab #define CEC_TX_CTRL           0x14
294be5e864SMauro Carvalho Chehab #define CEC_FREE_TIME_THRESH  0x18
304be5e864SMauro Carvalho Chehab #define CEC_BIT_TOUT_THRESH   0x1C
314be5e864SMauro Carvalho Chehab #define CEC_BIT_PULSE_THRESH  0x20
324be5e864SMauro Carvalho Chehab #define CEC_DATA              0x24
334be5e864SMauro Carvalho Chehab #define CEC_TX_ARRAY_CTRL     0x28
344be5e864SMauro Carvalho Chehab #define CEC_CTRL2             0x2C
354be5e864SMauro Carvalho Chehab #define CEC_TX_ERROR_STS      0x30
364be5e864SMauro Carvalho Chehab #define CEC_ADDR_TABLE        0x34
374be5e864SMauro Carvalho Chehab #define CEC_DATA_ARRAY_CTRL   0x38
384be5e864SMauro Carvalho Chehab #define CEC_DATA_ARRAY_STATUS 0x3C
394be5e864SMauro Carvalho Chehab #define CEC_TX_DATA_BASE      0x40
404be5e864SMauro Carvalho Chehab #define CEC_TX_DATA_TOP       0x50
414be5e864SMauro Carvalho Chehab #define CEC_TX_DATA_SIZE      0x1
424be5e864SMauro Carvalho Chehab #define CEC_RX_DATA_BASE      0x54
434be5e864SMauro Carvalho Chehab #define CEC_RX_DATA_TOP       0x64
444be5e864SMauro Carvalho Chehab #define CEC_RX_DATA_SIZE      0x1
454be5e864SMauro Carvalho Chehab 
464be5e864SMauro Carvalho Chehab /* CEC_CTRL2 */
474be5e864SMauro Carvalho Chehab #define CEC_LINE_INACTIVE_EN   BIT(0)
484be5e864SMauro Carvalho Chehab #define CEC_AUTO_BUS_ERR_EN    BIT(1)
494be5e864SMauro Carvalho Chehab #define CEC_STOP_ON_ARB_ERR_EN BIT(2)
504be5e864SMauro Carvalho Chehab #define CEC_TX_REQ_WAIT_EN     BIT(3)
514be5e864SMauro Carvalho Chehab 
524be5e864SMauro Carvalho Chehab /* CEC_DATA_ARRAY_CTRL */
534be5e864SMauro Carvalho Chehab #define CEC_TX_ARRAY_EN          BIT(0)
544be5e864SMauro Carvalho Chehab #define CEC_RX_ARRAY_EN          BIT(1)
554be5e864SMauro Carvalho Chehab #define CEC_TX_ARRAY_RESET       BIT(2)
564be5e864SMauro Carvalho Chehab #define CEC_RX_ARRAY_RESET       BIT(3)
574be5e864SMauro Carvalho Chehab #define CEC_TX_N_OF_BYTES_IRQ_EN BIT(4)
584be5e864SMauro Carvalho Chehab #define CEC_TX_STOP_ON_NACK      BIT(7)
594be5e864SMauro Carvalho Chehab 
604be5e864SMauro Carvalho Chehab /* CEC_TX_ARRAY_CTRL */
614be5e864SMauro Carvalho Chehab #define CEC_TX_N_OF_BYTES  0x1F
624be5e864SMauro Carvalho Chehab #define CEC_TX_START       BIT(5)
634be5e864SMauro Carvalho Chehab #define CEC_TX_AUTO_SOM_EN BIT(6)
644be5e864SMauro Carvalho Chehab #define CEC_TX_AUTO_EOM_EN BIT(7)
654be5e864SMauro Carvalho Chehab 
664be5e864SMauro Carvalho Chehab /* CEC_IRQ_CTRL */
674be5e864SMauro Carvalho Chehab #define CEC_TX_DONE_IRQ_EN   BIT(0)
684be5e864SMauro Carvalho Chehab #define CEC_ERROR_IRQ_EN     BIT(2)
694be5e864SMauro Carvalho Chehab #define CEC_RX_DONE_IRQ_EN   BIT(3)
704be5e864SMauro Carvalho Chehab #define CEC_RX_SOM_IRQ_EN    BIT(4)
714be5e864SMauro Carvalho Chehab #define CEC_RX_EOM_IRQ_EN    BIT(5)
724be5e864SMauro Carvalho Chehab #define CEC_FREE_TIME_IRQ_EN BIT(6)
734be5e864SMauro Carvalho Chehab #define CEC_PIN_STS_IRQ_EN   BIT(7)
744be5e864SMauro Carvalho Chehab 
754be5e864SMauro Carvalho Chehab /* CEC_CTRL */
764be5e864SMauro Carvalho Chehab #define CEC_IN_FILTER_EN    BIT(0)
774be5e864SMauro Carvalho Chehab #define CEC_PWR_SAVE_EN     BIT(1)
784be5e864SMauro Carvalho Chehab #define CEC_EN              BIT(4)
794be5e864SMauro Carvalho Chehab #define CEC_ACK_CTRL        BIT(5)
804be5e864SMauro Carvalho Chehab #define CEC_RX_RESET_EN     BIT(6)
814be5e864SMauro Carvalho Chehab #define CEC_IGNORE_RX_ERROR BIT(7)
824be5e864SMauro Carvalho Chehab 
834be5e864SMauro Carvalho Chehab /* CEC_STATUS */
844be5e864SMauro Carvalho Chehab #define CEC_TX_DONE_STS       BIT(0)
854be5e864SMauro Carvalho Chehab #define CEC_TX_ACK_GET_STS    BIT(1)
864be5e864SMauro Carvalho Chehab #define CEC_ERROR_STS         BIT(2)
874be5e864SMauro Carvalho Chehab #define CEC_RX_DONE_STS       BIT(3)
884be5e864SMauro Carvalho Chehab #define CEC_RX_SOM_STS        BIT(4)
894be5e864SMauro Carvalho Chehab #define CEC_RX_EOM_STS        BIT(5)
904be5e864SMauro Carvalho Chehab #define CEC_FREE_TIME_IRQ_STS BIT(6)
914be5e864SMauro Carvalho Chehab #define CEC_PIN_STS           BIT(7)
924be5e864SMauro Carvalho Chehab #define CEC_SBIT_TOUT_STS     BIT(8)
934be5e864SMauro Carvalho Chehab #define CEC_DBIT_TOUT_STS     BIT(9)
944be5e864SMauro Carvalho Chehab #define CEC_LPULSE_ERROR_STS  BIT(10)
954be5e864SMauro Carvalho Chehab #define CEC_HPULSE_ERROR_STS  BIT(11)
964be5e864SMauro Carvalho Chehab #define CEC_TX_ERROR          BIT(12)
974be5e864SMauro Carvalho Chehab #define CEC_TX_ARB_ERROR      BIT(13)
984be5e864SMauro Carvalho Chehab #define CEC_RX_ERROR_MIN      BIT(14)
994be5e864SMauro Carvalho Chehab #define CEC_RX_ERROR_MAX      BIT(15)
1004be5e864SMauro Carvalho Chehab 
1014be5e864SMauro Carvalho Chehab /* Signal free time in bit periods (2.4ms) */
1024be5e864SMauro Carvalho Chehab #define CEC_PRESENT_INIT_SFT 7
1034be5e864SMauro Carvalho Chehab #define CEC_NEW_INIT_SFT     5
1044be5e864SMauro Carvalho Chehab #define CEC_RETRANSMIT_SFT   3
1054be5e864SMauro Carvalho Chehab 
1064be5e864SMauro Carvalho Chehab /* Constants for CEC_BIT_TOUT_THRESH register */
1074be5e864SMauro Carvalho Chehab #define CEC_SBIT_TOUT_47MS BIT(1)
1084be5e864SMauro Carvalho Chehab #define CEC_SBIT_TOUT_48MS (BIT(0) | BIT(1))
1094be5e864SMauro Carvalho Chehab #define CEC_SBIT_TOUT_50MS BIT(2)
1104be5e864SMauro Carvalho Chehab #define CEC_DBIT_TOUT_27MS BIT(0)
1114be5e864SMauro Carvalho Chehab #define CEC_DBIT_TOUT_28MS BIT(1)
1124be5e864SMauro Carvalho Chehab #define CEC_DBIT_TOUT_29MS (BIT(0) | BIT(1))
1134be5e864SMauro Carvalho Chehab 
1144be5e864SMauro Carvalho Chehab /* Constants for CEC_BIT_PULSE_THRESH register */
1154be5e864SMauro Carvalho Chehab #define CEC_BIT_LPULSE_03MS BIT(1)
1164be5e864SMauro Carvalho Chehab #define CEC_BIT_HPULSE_03MS BIT(3)
1174be5e864SMauro Carvalho Chehab 
1184be5e864SMauro Carvalho Chehab /* Constants for CEC_DATA_ARRAY_STATUS register */
1194be5e864SMauro Carvalho Chehab #define CEC_RX_N_OF_BYTES                     0x1F
1204be5e864SMauro Carvalho Chehab #define CEC_TX_N_OF_BYTES_SENT                BIT(5)
1214be5e864SMauro Carvalho Chehab #define CEC_RX_OVERRUN                        BIT(6)
1224be5e864SMauro Carvalho Chehab 
1234be5e864SMauro Carvalho Chehab struct stih_cec {
1244be5e864SMauro Carvalho Chehab 	struct cec_adapter	*adap;
1254be5e864SMauro Carvalho Chehab 	struct device		*dev;
1264be5e864SMauro Carvalho Chehab 	struct clk		*clk;
1274be5e864SMauro Carvalho Chehab 	void __iomem		*regs;
1284be5e864SMauro Carvalho Chehab 	int			irq;
1294be5e864SMauro Carvalho Chehab 	u32			irq_status;
1304be5e864SMauro Carvalho Chehab 	struct cec_notifier	*notifier;
1314be5e864SMauro Carvalho Chehab };
1324be5e864SMauro Carvalho Chehab 
1334be5e864SMauro Carvalho Chehab static int stih_cec_adap_enable(struct cec_adapter *adap, bool enable)
1344be5e864SMauro Carvalho Chehab {
1354be5e864SMauro Carvalho Chehab 	struct stih_cec *cec = cec_get_drvdata(adap);
1364be5e864SMauro Carvalho Chehab 
1374be5e864SMauro Carvalho Chehab 	if (enable) {
1384be5e864SMauro Carvalho Chehab 		/* The doc says (input TCLK_PERIOD * CEC_CLK_DIV) = 0.1ms */
1394be5e864SMauro Carvalho Chehab 		unsigned long clk_freq = clk_get_rate(cec->clk);
1404be5e864SMauro Carvalho Chehab 		u32 cec_clk_div = clk_freq / 10000;
1414be5e864SMauro Carvalho Chehab 
1424be5e864SMauro Carvalho Chehab 		writel(cec_clk_div, cec->regs + CEC_CLK_DIV);
1434be5e864SMauro Carvalho Chehab 
1444be5e864SMauro Carvalho Chehab 		/* Configuration of the durations activating a timeout */
1454be5e864SMauro Carvalho Chehab 		writel(CEC_SBIT_TOUT_47MS | (CEC_DBIT_TOUT_28MS << 4),
1464be5e864SMauro Carvalho Chehab 		       cec->regs + CEC_BIT_TOUT_THRESH);
1474be5e864SMauro Carvalho Chehab 
1484be5e864SMauro Carvalho Chehab 		/* Configuration of the smallest allowed duration for pulses */
1494be5e864SMauro Carvalho Chehab 		writel(CEC_BIT_LPULSE_03MS | CEC_BIT_HPULSE_03MS,
1504be5e864SMauro Carvalho Chehab 		       cec->regs + CEC_BIT_PULSE_THRESH);
1514be5e864SMauro Carvalho Chehab 
1524be5e864SMauro Carvalho Chehab 		/* Minimum received bit period threshold */
1534be5e864SMauro Carvalho Chehab 		writel(BIT(5) | BIT(7), cec->regs + CEC_TX_CTRL);
1544be5e864SMauro Carvalho Chehab 
1554be5e864SMauro Carvalho Chehab 		/* Configuration of transceiver data arrays */
1564be5e864SMauro Carvalho Chehab 		writel(CEC_TX_ARRAY_EN | CEC_RX_ARRAY_EN | CEC_TX_STOP_ON_NACK,
1574be5e864SMauro Carvalho Chehab 		       cec->regs + CEC_DATA_ARRAY_CTRL);
1584be5e864SMauro Carvalho Chehab 
1594be5e864SMauro Carvalho Chehab 		/* Configuration of the control bits for CEC Transceiver */
1604be5e864SMauro Carvalho Chehab 		writel(CEC_IN_FILTER_EN | CEC_EN | CEC_RX_RESET_EN,
1614be5e864SMauro Carvalho Chehab 		       cec->regs + CEC_CTRL);
1624be5e864SMauro Carvalho Chehab 
1634be5e864SMauro Carvalho Chehab 		/* Clear logical addresses */
1644be5e864SMauro Carvalho Chehab 		writel(0, cec->regs + CEC_ADDR_TABLE);
1654be5e864SMauro Carvalho Chehab 
1664be5e864SMauro Carvalho Chehab 		/* Clear the status register */
1674be5e864SMauro Carvalho Chehab 		writel(0x0, cec->regs + CEC_STATUS);
1684be5e864SMauro Carvalho Chehab 
1694be5e864SMauro Carvalho Chehab 		/* Enable the interrupts */
1704be5e864SMauro Carvalho Chehab 		writel(CEC_TX_DONE_IRQ_EN | CEC_RX_DONE_IRQ_EN |
1714be5e864SMauro Carvalho Chehab 		       CEC_RX_SOM_IRQ_EN | CEC_RX_EOM_IRQ_EN |
1724be5e864SMauro Carvalho Chehab 		       CEC_ERROR_IRQ_EN,
1734be5e864SMauro Carvalho Chehab 		       cec->regs + CEC_IRQ_CTRL);
1744be5e864SMauro Carvalho Chehab 
1754be5e864SMauro Carvalho Chehab 	} else {
1764be5e864SMauro Carvalho Chehab 		/* Clear logical addresses */
1774be5e864SMauro Carvalho Chehab 		writel(0, cec->regs + CEC_ADDR_TABLE);
1784be5e864SMauro Carvalho Chehab 
1794be5e864SMauro Carvalho Chehab 		/* Clear the status register */
1804be5e864SMauro Carvalho Chehab 		writel(0x0, cec->regs + CEC_STATUS);
1814be5e864SMauro Carvalho Chehab 
1824be5e864SMauro Carvalho Chehab 		/* Disable the interrupts */
1834be5e864SMauro Carvalho Chehab 		writel(0, cec->regs + CEC_IRQ_CTRL);
1844be5e864SMauro Carvalho Chehab 	}
1854be5e864SMauro Carvalho Chehab 
1864be5e864SMauro Carvalho Chehab 	return 0;
1874be5e864SMauro Carvalho Chehab }
1884be5e864SMauro Carvalho Chehab 
1894be5e864SMauro Carvalho Chehab static int stih_cec_adap_log_addr(struct cec_adapter *adap, u8 logical_addr)
1904be5e864SMauro Carvalho Chehab {
1914be5e864SMauro Carvalho Chehab 	struct stih_cec *cec = cec_get_drvdata(adap);
1924be5e864SMauro Carvalho Chehab 	u32 reg = readl(cec->regs + CEC_ADDR_TABLE);
1934be5e864SMauro Carvalho Chehab 
1944be5e864SMauro Carvalho Chehab 	reg |= 1 << logical_addr;
1954be5e864SMauro Carvalho Chehab 
1964be5e864SMauro Carvalho Chehab 	if (logical_addr == CEC_LOG_ADDR_INVALID)
1974be5e864SMauro Carvalho Chehab 		reg = 0;
1984be5e864SMauro Carvalho Chehab 
1994be5e864SMauro Carvalho Chehab 	writel(reg, cec->regs + CEC_ADDR_TABLE);
2004be5e864SMauro Carvalho Chehab 
2014be5e864SMauro Carvalho Chehab 	return 0;
2024be5e864SMauro Carvalho Chehab }
2034be5e864SMauro Carvalho Chehab 
2044be5e864SMauro Carvalho Chehab static int stih_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
2054be5e864SMauro Carvalho Chehab 				  u32 signal_free_time, struct cec_msg *msg)
2064be5e864SMauro Carvalho Chehab {
2074be5e864SMauro Carvalho Chehab 	struct stih_cec *cec = cec_get_drvdata(adap);
2084be5e864SMauro Carvalho Chehab 	int i;
2094be5e864SMauro Carvalho Chehab 
2104be5e864SMauro Carvalho Chehab 	/* Copy message into registers */
2114be5e864SMauro Carvalho Chehab 	for (i = 0; i < msg->len; i++)
2124be5e864SMauro Carvalho Chehab 		writeb(msg->msg[i], cec->regs + CEC_TX_DATA_BASE + i);
2134be5e864SMauro Carvalho Chehab 
2144be5e864SMauro Carvalho Chehab 	/*
2154be5e864SMauro Carvalho Chehab 	 * Start transmission, configure hardware to add start and stop bits
2164be5e864SMauro Carvalho Chehab 	 * Signal free time is handled by the hardware
2174be5e864SMauro Carvalho Chehab 	 */
2184be5e864SMauro Carvalho Chehab 	writel(CEC_TX_AUTO_SOM_EN | CEC_TX_AUTO_EOM_EN | CEC_TX_START |
2194be5e864SMauro Carvalho Chehab 	       msg->len, cec->regs + CEC_TX_ARRAY_CTRL);
2204be5e864SMauro Carvalho Chehab 
2214be5e864SMauro Carvalho Chehab 	return 0;
2224be5e864SMauro Carvalho Chehab }
2234be5e864SMauro Carvalho Chehab 
2244be5e864SMauro Carvalho Chehab static void stih_tx_done(struct stih_cec *cec, u32 status)
2254be5e864SMauro Carvalho Chehab {
2264be5e864SMauro Carvalho Chehab 	if (status & CEC_TX_ERROR) {
2274be5e864SMauro Carvalho Chehab 		cec_transmit_attempt_done(cec->adap, CEC_TX_STATUS_ERROR);
2284be5e864SMauro Carvalho Chehab 		return;
2294be5e864SMauro Carvalho Chehab 	}
2304be5e864SMauro Carvalho Chehab 
2314be5e864SMauro Carvalho Chehab 	if (status & CEC_TX_ARB_ERROR) {
2324be5e864SMauro Carvalho Chehab 		cec_transmit_attempt_done(cec->adap, CEC_TX_STATUS_ARB_LOST);
2334be5e864SMauro Carvalho Chehab 		return;
2344be5e864SMauro Carvalho Chehab 	}
2354be5e864SMauro Carvalho Chehab 
2364be5e864SMauro Carvalho Chehab 	if (!(status & CEC_TX_ACK_GET_STS)) {
2374be5e864SMauro Carvalho Chehab 		cec_transmit_attempt_done(cec->adap, CEC_TX_STATUS_NACK);
2384be5e864SMauro Carvalho Chehab 		return;
2394be5e864SMauro Carvalho Chehab 	}
2404be5e864SMauro Carvalho Chehab 
2414be5e864SMauro Carvalho Chehab 	cec_transmit_attempt_done(cec->adap, CEC_TX_STATUS_OK);
2424be5e864SMauro Carvalho Chehab }
2434be5e864SMauro Carvalho Chehab 
2444be5e864SMauro Carvalho Chehab static void stih_rx_done(struct stih_cec *cec, u32 status)
2454be5e864SMauro Carvalho Chehab {
2464be5e864SMauro Carvalho Chehab 	struct cec_msg msg = {};
2474be5e864SMauro Carvalho Chehab 	u8 i;
2484be5e864SMauro Carvalho Chehab 
2494be5e864SMauro Carvalho Chehab 	if (status & CEC_RX_ERROR_MIN)
2504be5e864SMauro Carvalho Chehab 		return;
2514be5e864SMauro Carvalho Chehab 
2524be5e864SMauro Carvalho Chehab 	if (status & CEC_RX_ERROR_MAX)
2534be5e864SMauro Carvalho Chehab 		return;
2544be5e864SMauro Carvalho Chehab 
2554be5e864SMauro Carvalho Chehab 	msg.len = readl(cec->regs + CEC_DATA_ARRAY_STATUS) & 0x1f;
2564be5e864SMauro Carvalho Chehab 
2574be5e864SMauro Carvalho Chehab 	if (!msg.len)
2584be5e864SMauro Carvalho Chehab 		return;
2594be5e864SMauro Carvalho Chehab 
26005c480f4SHans Verkuil 	if (msg.len > CEC_MAX_MSG_SIZE)
26105c480f4SHans Verkuil 		msg.len = CEC_MAX_MSG_SIZE;
2624be5e864SMauro Carvalho Chehab 
2634be5e864SMauro Carvalho Chehab 	for (i = 0; i < msg.len; i++)
2644be5e864SMauro Carvalho Chehab 		msg.msg[i] = readl(cec->regs + CEC_RX_DATA_BASE + i);
2654be5e864SMauro Carvalho Chehab 
2664be5e864SMauro Carvalho Chehab 	cec_received_msg(cec->adap, &msg);
2674be5e864SMauro Carvalho Chehab }
2684be5e864SMauro Carvalho Chehab 
2694be5e864SMauro Carvalho Chehab static irqreturn_t stih_cec_irq_handler_thread(int irq, void *priv)
2704be5e864SMauro Carvalho Chehab {
2714be5e864SMauro Carvalho Chehab 	struct stih_cec *cec = priv;
2724be5e864SMauro Carvalho Chehab 
2734be5e864SMauro Carvalho Chehab 	if (cec->irq_status & CEC_TX_DONE_STS)
2744be5e864SMauro Carvalho Chehab 		stih_tx_done(cec, cec->irq_status);
2754be5e864SMauro Carvalho Chehab 
2764be5e864SMauro Carvalho Chehab 	if (cec->irq_status & CEC_RX_DONE_STS)
2774be5e864SMauro Carvalho Chehab 		stih_rx_done(cec, cec->irq_status);
2784be5e864SMauro Carvalho Chehab 
2794be5e864SMauro Carvalho Chehab 	cec->irq_status = 0;
2804be5e864SMauro Carvalho Chehab 
2814be5e864SMauro Carvalho Chehab 	return IRQ_HANDLED;
2824be5e864SMauro Carvalho Chehab }
2834be5e864SMauro Carvalho Chehab 
2844be5e864SMauro Carvalho Chehab static irqreturn_t stih_cec_irq_handler(int irq, void *priv)
2854be5e864SMauro Carvalho Chehab {
2864be5e864SMauro Carvalho Chehab 	struct stih_cec *cec = priv;
2874be5e864SMauro Carvalho Chehab 
2884be5e864SMauro Carvalho Chehab 	cec->irq_status = readl(cec->regs + CEC_STATUS);
2894be5e864SMauro Carvalho Chehab 	writel(cec->irq_status, cec->regs + CEC_STATUS);
2904be5e864SMauro Carvalho Chehab 
2914be5e864SMauro Carvalho Chehab 	return IRQ_WAKE_THREAD;
2924be5e864SMauro Carvalho Chehab }
2934be5e864SMauro Carvalho Chehab 
2944be5e864SMauro Carvalho Chehab static const struct cec_adap_ops sti_cec_adap_ops = {
2954be5e864SMauro Carvalho Chehab 	.adap_enable = stih_cec_adap_enable,
2964be5e864SMauro Carvalho Chehab 	.adap_log_addr = stih_cec_adap_log_addr,
2974be5e864SMauro Carvalho Chehab 	.adap_transmit = stih_cec_adap_transmit,
2984be5e864SMauro Carvalho Chehab };
2994be5e864SMauro Carvalho Chehab 
3004be5e864SMauro Carvalho Chehab static int stih_cec_probe(struct platform_device *pdev)
3014be5e864SMauro Carvalho Chehab {
3024be5e864SMauro Carvalho Chehab 	struct device *dev = &pdev->dev;
3034be5e864SMauro Carvalho Chehab 	struct stih_cec *cec;
3044be5e864SMauro Carvalho Chehab 	struct device *hdmi_dev;
3054be5e864SMauro Carvalho Chehab 	int ret;
3064be5e864SMauro Carvalho Chehab 
3074be5e864SMauro Carvalho Chehab 	hdmi_dev = cec_notifier_parse_hdmi_phandle(dev);
3084be5e864SMauro Carvalho Chehab 
3094be5e864SMauro Carvalho Chehab 	if (IS_ERR(hdmi_dev))
3104be5e864SMauro Carvalho Chehab 		return PTR_ERR(hdmi_dev);
3114be5e864SMauro Carvalho Chehab 
3124be5e864SMauro Carvalho Chehab 	cec = devm_kzalloc(dev, sizeof(*cec), GFP_KERNEL);
3134be5e864SMauro Carvalho Chehab 	if (!cec)
3144be5e864SMauro Carvalho Chehab 		return -ENOMEM;
3154be5e864SMauro Carvalho Chehab 
3164be5e864SMauro Carvalho Chehab 	cec->dev = dev;
3174be5e864SMauro Carvalho Chehab 
318beabb243SCai Huoqing 	cec->regs = devm_platform_ioremap_resource(pdev, 0);
3194be5e864SMauro Carvalho Chehab 	if (IS_ERR(cec->regs))
3204be5e864SMauro Carvalho Chehab 		return PTR_ERR(cec->regs);
3214be5e864SMauro Carvalho Chehab 
3224be5e864SMauro Carvalho Chehab 	cec->irq = platform_get_irq(pdev, 0);
3234be5e864SMauro Carvalho Chehab 	if (cec->irq < 0)
3244be5e864SMauro Carvalho Chehab 		return cec->irq;
3254be5e864SMauro Carvalho Chehab 
3264be5e864SMauro Carvalho Chehab 	ret = devm_request_threaded_irq(dev, cec->irq, stih_cec_irq_handler,
3274be5e864SMauro Carvalho Chehab 					stih_cec_irq_handler_thread, 0,
3284be5e864SMauro Carvalho Chehab 					pdev->name, cec);
3294be5e864SMauro Carvalho Chehab 	if (ret)
3304be5e864SMauro Carvalho Chehab 		return ret;
3314be5e864SMauro Carvalho Chehab 
3324be5e864SMauro Carvalho Chehab 	cec->clk = devm_clk_get(dev, "cec-clk");
3334be5e864SMauro Carvalho Chehab 	if (IS_ERR(cec->clk)) {
3344be5e864SMauro Carvalho Chehab 		dev_err(dev, "Cannot get cec clock\n");
3354be5e864SMauro Carvalho Chehab 		return PTR_ERR(cec->clk);
3364be5e864SMauro Carvalho Chehab 	}
3374be5e864SMauro Carvalho Chehab 
3384be5e864SMauro Carvalho Chehab 	cec->adap = cec_allocate_adapter(&sti_cec_adap_ops, cec, CEC_NAME,
3394be5e864SMauro Carvalho Chehab 					 CEC_CAP_DEFAULTS |
3404be5e864SMauro Carvalho Chehab 					 CEC_CAP_CONNECTOR_INFO,
3414be5e864SMauro Carvalho Chehab 					 CEC_MAX_LOG_ADDRS);
3424be5e864SMauro Carvalho Chehab 	ret = PTR_ERR_OR_ZERO(cec->adap);
3434be5e864SMauro Carvalho Chehab 	if (ret)
3444be5e864SMauro Carvalho Chehab 		return ret;
3454be5e864SMauro Carvalho Chehab 
3464be5e864SMauro Carvalho Chehab 	cec->notifier = cec_notifier_cec_adap_register(hdmi_dev, NULL,
3474be5e864SMauro Carvalho Chehab 						       cec->adap);
3484be5e864SMauro Carvalho Chehab 	if (!cec->notifier) {
3494be5e864SMauro Carvalho Chehab 		ret = -ENOMEM;
3504be5e864SMauro Carvalho Chehab 		goto err_delete_adapter;
3514be5e864SMauro Carvalho Chehab 	}
3524be5e864SMauro Carvalho Chehab 
3534be5e864SMauro Carvalho Chehab 	ret = cec_register_adapter(cec->adap, &pdev->dev);
3544be5e864SMauro Carvalho Chehab 	if (ret)
3554be5e864SMauro Carvalho Chehab 		goto err_notifier;
3564be5e864SMauro Carvalho Chehab 
3574be5e864SMauro Carvalho Chehab 	platform_set_drvdata(pdev, cec);
3584be5e864SMauro Carvalho Chehab 	return 0;
3594be5e864SMauro Carvalho Chehab 
3604be5e864SMauro Carvalho Chehab err_notifier:
3614be5e864SMauro Carvalho Chehab 	cec_notifier_cec_adap_unregister(cec->notifier, cec->adap);
3624be5e864SMauro Carvalho Chehab 
3634be5e864SMauro Carvalho Chehab err_delete_adapter:
3644be5e864SMauro Carvalho Chehab 	cec_delete_adapter(cec->adap);
3654be5e864SMauro Carvalho Chehab 	return ret;
3664be5e864SMauro Carvalho Chehab }
3674be5e864SMauro Carvalho Chehab 
3683cdae5bfSUwe Kleine-König static void stih_cec_remove(struct platform_device *pdev)
3694be5e864SMauro Carvalho Chehab {
3704be5e864SMauro Carvalho Chehab 	struct stih_cec *cec = platform_get_drvdata(pdev);
3714be5e864SMauro Carvalho Chehab 
3724be5e864SMauro Carvalho Chehab 	cec_notifier_cec_adap_unregister(cec->notifier, cec->adap);
3734be5e864SMauro Carvalho Chehab 	cec_unregister_adapter(cec->adap);
3744be5e864SMauro Carvalho Chehab }
3754be5e864SMauro Carvalho Chehab 
3764be5e864SMauro Carvalho Chehab static const struct of_device_id stih_cec_match[] = {
3774be5e864SMauro Carvalho Chehab 	{
3784be5e864SMauro Carvalho Chehab 		.compatible	= "st,stih-cec",
3794be5e864SMauro Carvalho Chehab 	},
3804be5e864SMauro Carvalho Chehab 	{},
3814be5e864SMauro Carvalho Chehab };
3824be5e864SMauro Carvalho Chehab MODULE_DEVICE_TABLE(of, stih_cec_match);
3834be5e864SMauro Carvalho Chehab 
3844be5e864SMauro Carvalho Chehab static struct platform_driver stih_cec_pdrv = {
3854be5e864SMauro Carvalho Chehab 	.probe	= stih_cec_probe,
3863cdae5bfSUwe Kleine-König 	.remove_new = stih_cec_remove,
3874be5e864SMauro Carvalho Chehab 	.driver = {
3884be5e864SMauro Carvalho Chehab 		.name		= CEC_NAME,
3894be5e864SMauro Carvalho Chehab 		.of_match_table	= stih_cec_match,
3904be5e864SMauro Carvalho Chehab 	},
3914be5e864SMauro Carvalho Chehab };
3924be5e864SMauro Carvalho Chehab 
3934be5e864SMauro Carvalho Chehab module_platform_driver(stih_cec_pdrv);
3944be5e864SMauro Carvalho Chehab 
3954be5e864SMauro Carvalho Chehab MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@linaro.org>");
3964be5e864SMauro Carvalho Chehab MODULE_LICENSE("GPL");
3974be5e864SMauro Carvalho Chehab MODULE_DESCRIPTION("STIH4xx CEC driver");
398