18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * STIH4xx CEC driver
48c2ecf20Sopenharmony_ci * Copyright (C) STMicroelectronics SA 2016
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/clk.h>
88c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/of.h>
138c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <media/cec.h>
178c2ecf20Sopenharmony_ci#include <media/cec-notifier.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define CEC_NAME	"stih-cec"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* CEC registers  */
228c2ecf20Sopenharmony_ci#define CEC_CLK_DIV           0x0
238c2ecf20Sopenharmony_ci#define CEC_CTRL              0x4
248c2ecf20Sopenharmony_ci#define CEC_IRQ_CTRL          0x8
258c2ecf20Sopenharmony_ci#define CEC_STATUS            0xC
268c2ecf20Sopenharmony_ci#define CEC_EXT_STATUS        0x10
278c2ecf20Sopenharmony_ci#define CEC_TX_CTRL           0x14
288c2ecf20Sopenharmony_ci#define CEC_FREE_TIME_THRESH  0x18
298c2ecf20Sopenharmony_ci#define CEC_BIT_TOUT_THRESH   0x1C
308c2ecf20Sopenharmony_ci#define CEC_BIT_PULSE_THRESH  0x20
318c2ecf20Sopenharmony_ci#define CEC_DATA              0x24
328c2ecf20Sopenharmony_ci#define CEC_TX_ARRAY_CTRL     0x28
338c2ecf20Sopenharmony_ci#define CEC_CTRL2             0x2C
348c2ecf20Sopenharmony_ci#define CEC_TX_ERROR_STS      0x30
358c2ecf20Sopenharmony_ci#define CEC_ADDR_TABLE        0x34
368c2ecf20Sopenharmony_ci#define CEC_DATA_ARRAY_CTRL   0x38
378c2ecf20Sopenharmony_ci#define CEC_DATA_ARRAY_STATUS 0x3C
388c2ecf20Sopenharmony_ci#define CEC_TX_DATA_BASE      0x40
398c2ecf20Sopenharmony_ci#define CEC_TX_DATA_TOP       0x50
408c2ecf20Sopenharmony_ci#define CEC_TX_DATA_SIZE      0x1
418c2ecf20Sopenharmony_ci#define CEC_RX_DATA_BASE      0x54
428c2ecf20Sopenharmony_ci#define CEC_RX_DATA_TOP       0x64
438c2ecf20Sopenharmony_ci#define CEC_RX_DATA_SIZE      0x1
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* CEC_CTRL2 */
468c2ecf20Sopenharmony_ci#define CEC_LINE_INACTIVE_EN   BIT(0)
478c2ecf20Sopenharmony_ci#define CEC_AUTO_BUS_ERR_EN    BIT(1)
488c2ecf20Sopenharmony_ci#define CEC_STOP_ON_ARB_ERR_EN BIT(2)
498c2ecf20Sopenharmony_ci#define CEC_TX_REQ_WAIT_EN     BIT(3)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* CEC_DATA_ARRAY_CTRL */
528c2ecf20Sopenharmony_ci#define CEC_TX_ARRAY_EN          BIT(0)
538c2ecf20Sopenharmony_ci#define CEC_RX_ARRAY_EN          BIT(1)
548c2ecf20Sopenharmony_ci#define CEC_TX_ARRAY_RESET       BIT(2)
558c2ecf20Sopenharmony_ci#define CEC_RX_ARRAY_RESET       BIT(3)
568c2ecf20Sopenharmony_ci#define CEC_TX_N_OF_BYTES_IRQ_EN BIT(4)
578c2ecf20Sopenharmony_ci#define CEC_TX_STOP_ON_NACK      BIT(7)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* CEC_TX_ARRAY_CTRL */
608c2ecf20Sopenharmony_ci#define CEC_TX_N_OF_BYTES  0x1F
618c2ecf20Sopenharmony_ci#define CEC_TX_START       BIT(5)
628c2ecf20Sopenharmony_ci#define CEC_TX_AUTO_SOM_EN BIT(6)
638c2ecf20Sopenharmony_ci#define CEC_TX_AUTO_EOM_EN BIT(7)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* CEC_IRQ_CTRL */
668c2ecf20Sopenharmony_ci#define CEC_TX_DONE_IRQ_EN   BIT(0)
678c2ecf20Sopenharmony_ci#define CEC_ERROR_IRQ_EN     BIT(2)
688c2ecf20Sopenharmony_ci#define CEC_RX_DONE_IRQ_EN   BIT(3)
698c2ecf20Sopenharmony_ci#define CEC_RX_SOM_IRQ_EN    BIT(4)
708c2ecf20Sopenharmony_ci#define CEC_RX_EOM_IRQ_EN    BIT(5)
718c2ecf20Sopenharmony_ci#define CEC_FREE_TIME_IRQ_EN BIT(6)
728c2ecf20Sopenharmony_ci#define CEC_PIN_STS_IRQ_EN   BIT(7)
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* CEC_CTRL */
758c2ecf20Sopenharmony_ci#define CEC_IN_FILTER_EN    BIT(0)
768c2ecf20Sopenharmony_ci#define CEC_PWR_SAVE_EN     BIT(1)
778c2ecf20Sopenharmony_ci#define CEC_EN              BIT(4)
788c2ecf20Sopenharmony_ci#define CEC_ACK_CTRL        BIT(5)
798c2ecf20Sopenharmony_ci#define CEC_RX_RESET_EN     BIT(6)
808c2ecf20Sopenharmony_ci#define CEC_IGNORE_RX_ERROR BIT(7)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* CEC_STATUS */
838c2ecf20Sopenharmony_ci#define CEC_TX_DONE_STS       BIT(0)
848c2ecf20Sopenharmony_ci#define CEC_TX_ACK_GET_STS    BIT(1)
858c2ecf20Sopenharmony_ci#define CEC_ERROR_STS         BIT(2)
868c2ecf20Sopenharmony_ci#define CEC_RX_DONE_STS       BIT(3)
878c2ecf20Sopenharmony_ci#define CEC_RX_SOM_STS        BIT(4)
888c2ecf20Sopenharmony_ci#define CEC_RX_EOM_STS        BIT(5)
898c2ecf20Sopenharmony_ci#define CEC_FREE_TIME_IRQ_STS BIT(6)
908c2ecf20Sopenharmony_ci#define CEC_PIN_STS           BIT(7)
918c2ecf20Sopenharmony_ci#define CEC_SBIT_TOUT_STS     BIT(8)
928c2ecf20Sopenharmony_ci#define CEC_DBIT_TOUT_STS     BIT(9)
938c2ecf20Sopenharmony_ci#define CEC_LPULSE_ERROR_STS  BIT(10)
948c2ecf20Sopenharmony_ci#define CEC_HPULSE_ERROR_STS  BIT(11)
958c2ecf20Sopenharmony_ci#define CEC_TX_ERROR          BIT(12)
968c2ecf20Sopenharmony_ci#define CEC_TX_ARB_ERROR      BIT(13)
978c2ecf20Sopenharmony_ci#define CEC_RX_ERROR_MIN      BIT(14)
988c2ecf20Sopenharmony_ci#define CEC_RX_ERROR_MAX      BIT(15)
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* Signal free time in bit periods (2.4ms) */
1018c2ecf20Sopenharmony_ci#define CEC_PRESENT_INIT_SFT 7
1028c2ecf20Sopenharmony_ci#define CEC_NEW_INIT_SFT     5
1038c2ecf20Sopenharmony_ci#define CEC_RETRANSMIT_SFT   3
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/* Constants for CEC_BIT_TOUT_THRESH register */
1068c2ecf20Sopenharmony_ci#define CEC_SBIT_TOUT_47MS BIT(1)
1078c2ecf20Sopenharmony_ci#define CEC_SBIT_TOUT_48MS (BIT(0) | BIT(1))
1088c2ecf20Sopenharmony_ci#define CEC_SBIT_TOUT_50MS BIT(2)
1098c2ecf20Sopenharmony_ci#define CEC_DBIT_TOUT_27MS BIT(0)
1108c2ecf20Sopenharmony_ci#define CEC_DBIT_TOUT_28MS BIT(1)
1118c2ecf20Sopenharmony_ci#define CEC_DBIT_TOUT_29MS (BIT(0) | BIT(1))
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/* Constants for CEC_BIT_PULSE_THRESH register */
1148c2ecf20Sopenharmony_ci#define CEC_BIT_LPULSE_03MS BIT(1)
1158c2ecf20Sopenharmony_ci#define CEC_BIT_HPULSE_03MS BIT(3)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* Constants for CEC_DATA_ARRAY_STATUS register */
1188c2ecf20Sopenharmony_ci#define CEC_RX_N_OF_BYTES                     0x1F
1198c2ecf20Sopenharmony_ci#define CEC_TX_N_OF_BYTES_SENT                BIT(5)
1208c2ecf20Sopenharmony_ci#define CEC_RX_OVERRUN                        BIT(6)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistruct stih_cec {
1238c2ecf20Sopenharmony_ci	struct cec_adapter	*adap;
1248c2ecf20Sopenharmony_ci	struct device		*dev;
1258c2ecf20Sopenharmony_ci	struct clk		*clk;
1268c2ecf20Sopenharmony_ci	void __iomem		*regs;
1278c2ecf20Sopenharmony_ci	int			irq;
1288c2ecf20Sopenharmony_ci	u32			irq_status;
1298c2ecf20Sopenharmony_ci	struct cec_notifier	*notifier;
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic int stih_cec_adap_enable(struct cec_adapter *adap, bool enable)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct stih_cec *cec = cec_get_drvdata(adap);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (enable) {
1378c2ecf20Sopenharmony_ci		/* The doc says (input TCLK_PERIOD * CEC_CLK_DIV) = 0.1ms */
1388c2ecf20Sopenharmony_ci		unsigned long clk_freq = clk_get_rate(cec->clk);
1398c2ecf20Sopenharmony_ci		u32 cec_clk_div = clk_freq / 10000;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		writel(cec_clk_div, cec->regs + CEC_CLK_DIV);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci		/* Configuration of the durations activating a timeout */
1448c2ecf20Sopenharmony_ci		writel(CEC_SBIT_TOUT_47MS | (CEC_DBIT_TOUT_28MS << 4),
1458c2ecf20Sopenharmony_ci		       cec->regs + CEC_BIT_TOUT_THRESH);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci		/* Configuration of the smallest allowed duration for pulses */
1488c2ecf20Sopenharmony_ci		writel(CEC_BIT_LPULSE_03MS | CEC_BIT_HPULSE_03MS,
1498c2ecf20Sopenharmony_ci		       cec->regs + CEC_BIT_PULSE_THRESH);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		/* Minimum received bit period threshold */
1528c2ecf20Sopenharmony_ci		writel(BIT(5) | BIT(7), cec->regs + CEC_TX_CTRL);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci		/* Configuration of transceiver data arrays */
1558c2ecf20Sopenharmony_ci		writel(CEC_TX_ARRAY_EN | CEC_RX_ARRAY_EN | CEC_TX_STOP_ON_NACK,
1568c2ecf20Sopenharmony_ci		       cec->regs + CEC_DATA_ARRAY_CTRL);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci		/* Configuration of the control bits for CEC Transceiver */
1598c2ecf20Sopenharmony_ci		writel(CEC_IN_FILTER_EN | CEC_EN | CEC_RX_RESET_EN,
1608c2ecf20Sopenharmony_ci		       cec->regs + CEC_CTRL);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		/* Clear logical addresses */
1638c2ecf20Sopenharmony_ci		writel(0, cec->regs + CEC_ADDR_TABLE);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		/* Clear the status register */
1668c2ecf20Sopenharmony_ci		writel(0x0, cec->regs + CEC_STATUS);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci		/* Enable the interrupts */
1698c2ecf20Sopenharmony_ci		writel(CEC_TX_DONE_IRQ_EN | CEC_RX_DONE_IRQ_EN |
1708c2ecf20Sopenharmony_ci		       CEC_RX_SOM_IRQ_EN | CEC_RX_EOM_IRQ_EN |
1718c2ecf20Sopenharmony_ci		       CEC_ERROR_IRQ_EN,
1728c2ecf20Sopenharmony_ci		       cec->regs + CEC_IRQ_CTRL);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	} else {
1758c2ecf20Sopenharmony_ci		/* Clear logical addresses */
1768c2ecf20Sopenharmony_ci		writel(0, cec->regs + CEC_ADDR_TABLE);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci		/* Clear the status register */
1798c2ecf20Sopenharmony_ci		writel(0x0, cec->regs + CEC_STATUS);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci		/* Disable the interrupts */
1828c2ecf20Sopenharmony_ci		writel(0, cec->regs + CEC_IRQ_CTRL);
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	return 0;
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic int stih_cec_adap_log_addr(struct cec_adapter *adap, u8 logical_addr)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct stih_cec *cec = cec_get_drvdata(adap);
1918c2ecf20Sopenharmony_ci	u32 reg = readl(cec->regs + CEC_ADDR_TABLE);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	reg |= 1 << logical_addr;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	if (logical_addr == CEC_LOG_ADDR_INVALID)
1968c2ecf20Sopenharmony_ci		reg = 0;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	writel(reg, cec->regs + CEC_ADDR_TABLE);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return 0;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic int stih_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
2048c2ecf20Sopenharmony_ci				  u32 signal_free_time, struct cec_msg *msg)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	struct stih_cec *cec = cec_get_drvdata(adap);
2078c2ecf20Sopenharmony_ci	int i;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	/* Copy message into registers */
2108c2ecf20Sopenharmony_ci	for (i = 0; i < msg->len; i++)
2118c2ecf20Sopenharmony_ci		writeb(msg->msg[i], cec->regs + CEC_TX_DATA_BASE + i);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/*
2148c2ecf20Sopenharmony_ci	 * Start transmission, configure hardware to add start and stop bits
2158c2ecf20Sopenharmony_ci	 * Signal free time is handled by the hardware
2168c2ecf20Sopenharmony_ci	 */
2178c2ecf20Sopenharmony_ci	writel(CEC_TX_AUTO_SOM_EN | CEC_TX_AUTO_EOM_EN | CEC_TX_START |
2188c2ecf20Sopenharmony_ci	       msg->len, cec->regs + CEC_TX_ARRAY_CTRL);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	return 0;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cistatic void stih_tx_done(struct stih_cec *cec, u32 status)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	if (status & CEC_TX_ERROR) {
2268c2ecf20Sopenharmony_ci		cec_transmit_attempt_done(cec->adap, CEC_TX_STATUS_ERROR);
2278c2ecf20Sopenharmony_ci		return;
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	if (status & CEC_TX_ARB_ERROR) {
2318c2ecf20Sopenharmony_ci		cec_transmit_attempt_done(cec->adap, CEC_TX_STATUS_ARB_LOST);
2328c2ecf20Sopenharmony_ci		return;
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	if (!(status & CEC_TX_ACK_GET_STS)) {
2368c2ecf20Sopenharmony_ci		cec_transmit_attempt_done(cec->adap, CEC_TX_STATUS_NACK);
2378c2ecf20Sopenharmony_ci		return;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	cec_transmit_attempt_done(cec->adap, CEC_TX_STATUS_OK);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic void stih_rx_done(struct stih_cec *cec, u32 status)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	struct cec_msg msg = {};
2468c2ecf20Sopenharmony_ci	u8 i;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	if (status & CEC_RX_ERROR_MIN)
2498c2ecf20Sopenharmony_ci		return;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	if (status & CEC_RX_ERROR_MAX)
2528c2ecf20Sopenharmony_ci		return;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	msg.len = readl(cec->regs + CEC_DATA_ARRAY_STATUS) & 0x1f;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (!msg.len)
2578c2ecf20Sopenharmony_ci		return;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	if (msg.len > 16)
2608c2ecf20Sopenharmony_ci		msg.len = 16;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	for (i = 0; i < msg.len; i++)
2638c2ecf20Sopenharmony_ci		msg.msg[i] = readl(cec->regs + CEC_RX_DATA_BASE + i);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	cec_received_msg(cec->adap, &msg);
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic irqreturn_t stih_cec_irq_handler_thread(int irq, void *priv)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	struct stih_cec *cec = priv;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	if (cec->irq_status & CEC_TX_DONE_STS)
2738c2ecf20Sopenharmony_ci		stih_tx_done(cec, cec->irq_status);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (cec->irq_status & CEC_RX_DONE_STS)
2768c2ecf20Sopenharmony_ci		stih_rx_done(cec, cec->irq_status);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	cec->irq_status = 0;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic irqreturn_t stih_cec_irq_handler(int irq, void *priv)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	struct stih_cec *cec = priv;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	cec->irq_status = readl(cec->regs + CEC_STATUS);
2888c2ecf20Sopenharmony_ci	writel(cec->irq_status, cec->regs + CEC_STATUS);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	return IRQ_WAKE_THREAD;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic const struct cec_adap_ops sti_cec_adap_ops = {
2948c2ecf20Sopenharmony_ci	.adap_enable = stih_cec_adap_enable,
2958c2ecf20Sopenharmony_ci	.adap_log_addr = stih_cec_adap_log_addr,
2968c2ecf20Sopenharmony_ci	.adap_transmit = stih_cec_adap_transmit,
2978c2ecf20Sopenharmony_ci};
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic int stih_cec_probe(struct platform_device *pdev)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
3028c2ecf20Sopenharmony_ci	struct resource *res;
3038c2ecf20Sopenharmony_ci	struct stih_cec *cec;
3048c2ecf20Sopenharmony_ci	struct device *hdmi_dev;
3058c2ecf20Sopenharmony_ci	int ret;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	hdmi_dev = cec_notifier_parse_hdmi_phandle(dev);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	if (IS_ERR(hdmi_dev))
3108c2ecf20Sopenharmony_ci		return PTR_ERR(hdmi_dev);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	cec = devm_kzalloc(dev, sizeof(*cec), GFP_KERNEL);
3138c2ecf20Sopenharmony_ci	if (!cec)
3148c2ecf20Sopenharmony_ci		return -ENOMEM;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	cec->dev = dev;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3198c2ecf20Sopenharmony_ci	cec->regs = devm_ioremap_resource(dev, res);
3208c2ecf20Sopenharmony_ci	if (IS_ERR(cec->regs))
3218c2ecf20Sopenharmony_ci		return PTR_ERR(cec->regs);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	cec->irq = platform_get_irq(pdev, 0);
3248c2ecf20Sopenharmony_ci	if (cec->irq < 0)
3258c2ecf20Sopenharmony_ci		return cec->irq;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	ret = devm_request_threaded_irq(dev, cec->irq, stih_cec_irq_handler,
3288c2ecf20Sopenharmony_ci					stih_cec_irq_handler_thread, 0,
3298c2ecf20Sopenharmony_ci					pdev->name, cec);
3308c2ecf20Sopenharmony_ci	if (ret)
3318c2ecf20Sopenharmony_ci		return ret;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	cec->clk = devm_clk_get(dev, "cec-clk");
3348c2ecf20Sopenharmony_ci	if (IS_ERR(cec->clk)) {
3358c2ecf20Sopenharmony_ci		dev_err(dev, "Cannot get cec clock\n");
3368c2ecf20Sopenharmony_ci		return PTR_ERR(cec->clk);
3378c2ecf20Sopenharmony_ci	}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	cec->adap = cec_allocate_adapter(&sti_cec_adap_ops, cec, CEC_NAME,
3408c2ecf20Sopenharmony_ci					 CEC_CAP_DEFAULTS |
3418c2ecf20Sopenharmony_ci					 CEC_CAP_CONNECTOR_INFO,
3428c2ecf20Sopenharmony_ci					 CEC_MAX_LOG_ADDRS);
3438c2ecf20Sopenharmony_ci	ret = PTR_ERR_OR_ZERO(cec->adap);
3448c2ecf20Sopenharmony_ci	if (ret)
3458c2ecf20Sopenharmony_ci		return ret;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	cec->notifier = cec_notifier_cec_adap_register(hdmi_dev, NULL,
3488c2ecf20Sopenharmony_ci						       cec->adap);
3498c2ecf20Sopenharmony_ci	if (!cec->notifier) {
3508c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3518c2ecf20Sopenharmony_ci		goto err_delete_adapter;
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	ret = cec_register_adapter(cec->adap, &pdev->dev);
3558c2ecf20Sopenharmony_ci	if (ret)
3568c2ecf20Sopenharmony_ci		goto err_notifier;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, cec);
3598c2ecf20Sopenharmony_ci	return 0;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cierr_notifier:
3628c2ecf20Sopenharmony_ci	cec_notifier_cec_adap_unregister(cec->notifier, cec->adap);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cierr_delete_adapter:
3658c2ecf20Sopenharmony_ci	cec_delete_adapter(cec->adap);
3668c2ecf20Sopenharmony_ci	return ret;
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic int stih_cec_remove(struct platform_device *pdev)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	struct stih_cec *cec = platform_get_drvdata(pdev);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	cec_notifier_cec_adap_unregister(cec->notifier, cec->adap);
3748c2ecf20Sopenharmony_ci	cec_unregister_adapter(cec->adap);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	return 0;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic const struct of_device_id stih_cec_match[] = {
3808c2ecf20Sopenharmony_ci	{
3818c2ecf20Sopenharmony_ci		.compatible	= "st,stih-cec",
3828c2ecf20Sopenharmony_ci	},
3838c2ecf20Sopenharmony_ci	{},
3848c2ecf20Sopenharmony_ci};
3858c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, stih_cec_match);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic struct platform_driver stih_cec_pdrv = {
3888c2ecf20Sopenharmony_ci	.probe	= stih_cec_probe,
3898c2ecf20Sopenharmony_ci	.remove = stih_cec_remove,
3908c2ecf20Sopenharmony_ci	.driver = {
3918c2ecf20Sopenharmony_ci		.name		= CEC_NAME,
3928c2ecf20Sopenharmony_ci		.of_match_table	= stih_cec_match,
3938c2ecf20Sopenharmony_ci	},
3948c2ecf20Sopenharmony_ci};
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cimodule_platform_driver(stih_cec_pdrv);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ciMODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@linaro.org>");
3998c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4008c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("STIH4xx CEC driver");
401