Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / platform / s5p-cec / s5p_cec.c
blob8837e2678bdeb3507e4ebce501e5ee58216334ad
1 /* drivers/media/platform/s5p-cec/s5p_cec.c
3 * Samsung S5P CEC driver
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is based on the "cec interface driver for exynos soc" by
13 * SangPil Moon.
16 #include <linux/clk.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/timer.h>
26 #include <linux/workqueue.h>
27 #include <media/cec.h>
28 #include <media/cec-notifier.h>
30 #include "exynos_hdmi_cec.h"
31 #include "regs-cec.h"
32 #include "s5p_cec.h"
34 #define CEC_NAME "s5p-cec"
36 static int debug;
37 module_param(debug, int, 0644);
38 MODULE_PARM_DESC(debug, "debug level (0-2)");
40 static int s5p_cec_adap_enable(struct cec_adapter *adap, bool enable)
42 struct s5p_cec_dev *cec = cec_get_drvdata(adap);
44 if (enable) {
45 pm_runtime_get_sync(cec->dev);
47 s5p_cec_reset(cec);
49 s5p_cec_set_divider(cec);
50 s5p_cec_threshold(cec);
52 s5p_cec_unmask_tx_interrupts(cec);
53 s5p_cec_unmask_rx_interrupts(cec);
54 s5p_cec_enable_rx(cec);
55 } else {
56 s5p_cec_mask_tx_interrupts(cec);
57 s5p_cec_mask_rx_interrupts(cec);
58 pm_runtime_disable(cec->dev);
61 return 0;
64 static int s5p_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
66 struct s5p_cec_dev *cec = cec_get_drvdata(adap);
68 s5p_cec_set_addr(cec, addr);
69 return 0;
72 static int s5p_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
73 u32 signal_free_time, struct cec_msg *msg)
75 struct s5p_cec_dev *cec = cec_get_drvdata(adap);
78 * Unclear if 0 retries are allowed by the hardware, so have 1 as
79 * the minimum.
81 s5p_cec_copy_packet(cec, msg->msg, msg->len, max(1, attempts - 1));
82 return 0;
85 static irqreturn_t s5p_cec_irq_handler(int irq, void *priv)
87 struct s5p_cec_dev *cec = priv;
88 u32 status = 0;
90 status = s5p_cec_get_status(cec);
92 dev_dbg(cec->dev, "irq received\n");
94 if (status & CEC_STATUS_TX_DONE) {
95 if (status & CEC_STATUS_TX_NACK) {
96 dev_dbg(cec->dev, "CEC_STATUS_TX_NACK set\n");
97 cec->tx = STATE_NACK;
98 } else if (status & CEC_STATUS_TX_ERROR) {
99 dev_dbg(cec->dev, "CEC_STATUS_TX_ERROR set\n");
100 cec->tx = STATE_ERROR;
101 } else {
102 dev_dbg(cec->dev, "CEC_STATUS_TX_DONE\n");
103 cec->tx = STATE_DONE;
105 s5p_clr_pending_tx(cec);
108 if (status & CEC_STATUS_RX_DONE) {
109 if (status & CEC_STATUS_RX_ERROR) {
110 dev_dbg(cec->dev, "CEC_STATUS_RX_ERROR set\n");
111 s5p_cec_rx_reset(cec);
112 s5p_cec_enable_rx(cec);
113 } else {
114 dev_dbg(cec->dev, "CEC_STATUS_RX_DONE set\n");
115 if (cec->rx != STATE_IDLE)
116 dev_dbg(cec->dev, "Buffer overrun (worker did not process previous message)\n");
117 cec->rx = STATE_BUSY;
118 cec->msg.len = status >> 24;
119 cec->msg.rx_status = CEC_RX_STATUS_OK;
120 s5p_cec_get_rx_buf(cec, cec->msg.len,
121 cec->msg.msg);
122 cec->rx = STATE_DONE;
123 s5p_cec_enable_rx(cec);
125 /* Clear interrupt pending bit */
126 s5p_clr_pending_rx(cec);
128 return IRQ_WAKE_THREAD;
131 static irqreturn_t s5p_cec_irq_handler_thread(int irq, void *priv)
133 struct s5p_cec_dev *cec = priv;
135 dev_dbg(cec->dev, "irq processing thread\n");
136 switch (cec->tx) {
137 case STATE_DONE:
138 cec_transmit_done(cec->adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
139 cec->tx = STATE_IDLE;
140 break;
141 case STATE_NACK:
142 cec_transmit_done(cec->adap,
143 CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_NACK,
144 0, 1, 0, 0);
145 cec->tx = STATE_IDLE;
146 break;
147 case STATE_ERROR:
148 cec_transmit_done(cec->adap,
149 CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_ERROR,
150 0, 0, 0, 1);
151 cec->tx = STATE_IDLE;
152 break;
153 case STATE_BUSY:
154 dev_err(cec->dev, "state set to busy, this should not occur here\n");
155 break;
156 default:
157 break;
160 switch (cec->rx) {
161 case STATE_DONE:
162 cec_received_msg(cec->adap, &cec->msg);
163 cec->rx = STATE_IDLE;
164 break;
165 default:
166 break;
169 return IRQ_HANDLED;
172 static const struct cec_adap_ops s5p_cec_adap_ops = {
173 .adap_enable = s5p_cec_adap_enable,
174 .adap_log_addr = s5p_cec_adap_log_addr,
175 .adap_transmit = s5p_cec_adap_transmit,
178 static int s5p_cec_probe(struct platform_device *pdev)
180 struct device *dev = &pdev->dev;
181 struct device_node *np;
182 struct platform_device *hdmi_dev;
183 struct resource *res;
184 struct s5p_cec_dev *cec;
185 bool needs_hpd = of_property_read_bool(pdev->dev.of_node, "needs-hpd");
186 int ret;
188 np = of_parse_phandle(pdev->dev.of_node, "hdmi-phandle", 0);
190 if (!np) {
191 dev_err(&pdev->dev, "Failed to find hdmi node in device tree\n");
192 return -ENODEV;
194 hdmi_dev = of_find_device_by_node(np);
195 if (hdmi_dev == NULL)
196 return -EPROBE_DEFER;
198 cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL);
199 if (!cec)
200 return -ENOMEM;
202 cec->dev = dev;
204 cec->irq = platform_get_irq(pdev, 0);
205 if (cec->irq < 0)
206 return cec->irq;
208 ret = devm_request_threaded_irq(dev, cec->irq, s5p_cec_irq_handler,
209 s5p_cec_irq_handler_thread, 0, pdev->name, cec);
210 if (ret)
211 return ret;
213 cec->clk = devm_clk_get(dev, "hdmicec");
214 if (IS_ERR(cec->clk))
215 return PTR_ERR(cec->clk);
217 cec->pmu = syscon_regmap_lookup_by_phandle(dev->of_node,
218 "samsung,syscon-phandle");
219 if (IS_ERR(cec->pmu))
220 return -EPROBE_DEFER;
222 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
223 cec->reg = devm_ioremap_resource(dev, res);
224 if (IS_ERR(cec->reg))
225 return PTR_ERR(cec->reg);
227 cec->notifier = cec_notifier_get(&hdmi_dev->dev);
228 if (cec->notifier == NULL)
229 return -ENOMEM;
231 cec->adap = cec_allocate_adapter(&s5p_cec_adap_ops, cec, CEC_NAME,
232 CEC_CAP_DEFAULTS | (needs_hpd ? CEC_CAP_NEEDS_HPD : 0), 1);
233 ret = PTR_ERR_OR_ZERO(cec->adap);
234 if (ret)
235 return ret;
237 ret = cec_register_adapter(cec->adap, &pdev->dev);
238 if (ret)
239 goto err_delete_adapter;
241 cec_register_cec_notifier(cec->adap, cec->notifier);
243 platform_set_drvdata(pdev, cec);
244 pm_runtime_enable(dev);
246 dev_dbg(dev, "successfully probed\n");
247 return 0;
249 err_delete_adapter:
250 cec_delete_adapter(cec->adap);
251 return ret;
254 static int s5p_cec_remove(struct platform_device *pdev)
256 struct s5p_cec_dev *cec = platform_get_drvdata(pdev);
258 cec_unregister_adapter(cec->adap);
259 cec_notifier_put(cec->notifier);
260 pm_runtime_disable(&pdev->dev);
261 return 0;
264 static int __maybe_unused s5p_cec_runtime_suspend(struct device *dev)
266 struct s5p_cec_dev *cec = dev_get_drvdata(dev);
268 clk_disable_unprepare(cec->clk);
269 return 0;
272 static int __maybe_unused s5p_cec_runtime_resume(struct device *dev)
274 struct s5p_cec_dev *cec = dev_get_drvdata(dev);
275 int ret;
277 ret = clk_prepare_enable(cec->clk);
278 if (ret < 0)
279 return ret;
280 return 0;
283 static const struct dev_pm_ops s5p_cec_pm_ops = {
284 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
285 pm_runtime_force_resume)
286 SET_RUNTIME_PM_OPS(s5p_cec_runtime_suspend, s5p_cec_runtime_resume,
287 NULL)
290 static const struct of_device_id s5p_cec_match[] = {
292 .compatible = "samsung,s5p-cec",
296 MODULE_DEVICE_TABLE(of, s5p_cec_match);
298 static struct platform_driver s5p_cec_pdrv = {
299 .probe = s5p_cec_probe,
300 .remove = s5p_cec_remove,
301 .driver = {
302 .name = CEC_NAME,
303 .of_match_table = s5p_cec_match,
304 .pm = &s5p_cec_pm_ops,
308 module_platform_driver(s5p_cec_pdrv);
310 MODULE_AUTHOR("Kamil Debski <kamil@wypas.org>");
311 MODULE_LICENSE("GPL");
312 MODULE_DESCRIPTION("Samsung S5P CEC driver");