Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 1 | /* drivers/media/platform/s5p-cec/s5p_cec.c |
| 2 | * |
| 3 | * Samsung S5P CEC driver |
| 4 | * |
| 5 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. |
| 6 | * |
| 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. |
| 11 | * |
| 12 | * This driver is based on the "cec interface driver for exynos soc" by |
| 13 | * SangPil Moon. |
| 14 | */ |
| 15 | |
| 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> |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 22 | #include <linux/of_platform.h> |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/pm_runtime.h> |
| 25 | #include <linux/timer.h> |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 26 | #include <linux/workqueue.h> |
| 27 | #include <media/cec.h> |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 28 | #include <media/cec-notifier.h> |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 29 | |
| 30 | #include "exynos_hdmi_cec.h" |
| 31 | #include "regs-cec.h" |
| 32 | #include "s5p_cec.h" |
| 33 | |
| 34 | #define CEC_NAME "s5p-cec" |
| 35 | |
| 36 | static int debug; |
| 37 | module_param(debug, int, 0644); |
| 38 | MODULE_PARM_DESC(debug, "debug level (0-2)"); |
| 39 | |
| 40 | static int s5p_cec_adap_enable(struct cec_adapter *adap, bool enable) |
| 41 | { |
Jose Abreu | 91f6d55 | 2017-03-24 13:47:54 -0300 | [diff] [blame] | 42 | struct s5p_cec_dev *cec = cec_get_drvdata(adap); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 43 | |
| 44 | if (enable) { |
Mauro Carvalho Chehab | c51202d | 2016-06-28 11:44:06 -0300 | [diff] [blame] | 45 | pm_runtime_get_sync(cec->dev); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 46 | |
| 47 | s5p_cec_reset(cec); |
| 48 | |
| 49 | s5p_cec_set_divider(cec); |
| 50 | s5p_cec_threshold(cec); |
| 51 | |
| 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); |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int s5p_cec_adap_log_addr(struct cec_adapter *adap, u8 addr) |
| 65 | { |
Jose Abreu | 91f6d55 | 2017-03-24 13:47:54 -0300 | [diff] [blame] | 66 | struct s5p_cec_dev *cec = cec_get_drvdata(adap); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 67 | |
| 68 | s5p_cec_set_addr(cec, addr); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int s5p_cec_adap_transmit(struct cec_adapter *adap, u8 attempts, |
| 73 | u32 signal_free_time, struct cec_msg *msg) |
| 74 | { |
Jose Abreu | 91f6d55 | 2017-03-24 13:47:54 -0300 | [diff] [blame] | 75 | struct s5p_cec_dev *cec = cec_get_drvdata(adap); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Unclear if 0 retries are allowed by the hardware, so have 1 as |
| 79 | * the minimum. |
| 80 | */ |
| 81 | s5p_cec_copy_packet(cec, msg->msg, msg->len, max(1, attempts - 1)); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static irqreturn_t s5p_cec_irq_handler(int irq, void *priv) |
| 86 | { |
| 87 | struct s5p_cec_dev *cec = priv; |
| 88 | u32 status = 0; |
| 89 | |
| 90 | status = s5p_cec_get_status(cec); |
| 91 | |
| 92 | dev_dbg(cec->dev, "irq received\n"); |
| 93 | |
| 94 | if (status & CEC_STATUS_TX_DONE) { |
| 95 | if (status & CEC_STATUS_TX_ERROR) { |
| 96 | dev_dbg(cec->dev, "CEC_STATUS_TX_ERROR set\n"); |
| 97 | cec->tx = STATE_ERROR; |
| 98 | } else { |
| 99 | dev_dbg(cec->dev, "CEC_STATUS_TX_DONE\n"); |
| 100 | cec->tx = STATE_DONE; |
| 101 | } |
| 102 | s5p_clr_pending_tx(cec); |
| 103 | } |
| 104 | |
| 105 | if (status & CEC_STATUS_RX_DONE) { |
| 106 | if (status & CEC_STATUS_RX_ERROR) { |
| 107 | dev_dbg(cec->dev, "CEC_STATUS_RX_ERROR set\n"); |
| 108 | s5p_cec_rx_reset(cec); |
| 109 | s5p_cec_enable_rx(cec); |
| 110 | } else { |
| 111 | dev_dbg(cec->dev, "CEC_STATUS_RX_DONE set\n"); |
| 112 | if (cec->rx != STATE_IDLE) |
| 113 | dev_dbg(cec->dev, "Buffer overrun (worker did not process previous message)\n"); |
| 114 | cec->rx = STATE_BUSY; |
| 115 | cec->msg.len = status >> 24; |
| 116 | cec->msg.rx_status = CEC_RX_STATUS_OK; |
| 117 | s5p_cec_get_rx_buf(cec, cec->msg.len, |
| 118 | cec->msg.msg); |
| 119 | cec->rx = STATE_DONE; |
| 120 | s5p_cec_enable_rx(cec); |
| 121 | } |
| 122 | /* Clear interrupt pending bit */ |
| 123 | s5p_clr_pending_rx(cec); |
| 124 | } |
| 125 | return IRQ_WAKE_THREAD; |
| 126 | } |
| 127 | |
| 128 | static irqreturn_t s5p_cec_irq_handler_thread(int irq, void *priv) |
| 129 | { |
| 130 | struct s5p_cec_dev *cec = priv; |
| 131 | |
| 132 | dev_dbg(cec->dev, "irq processing thread\n"); |
| 133 | switch (cec->tx) { |
| 134 | case STATE_DONE: |
| 135 | cec_transmit_done(cec->adap, CEC_TX_STATUS_OK, 0, 0, 0, 0); |
| 136 | cec->tx = STATE_IDLE; |
| 137 | break; |
| 138 | case STATE_ERROR: |
| 139 | cec_transmit_done(cec->adap, |
| 140 | CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_ERROR, |
| 141 | 0, 0, 0, 1); |
| 142 | cec->tx = STATE_IDLE; |
| 143 | break; |
| 144 | case STATE_BUSY: |
| 145 | dev_err(cec->dev, "state set to busy, this should not occur here\n"); |
| 146 | break; |
| 147 | default: |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | switch (cec->rx) { |
| 152 | case STATE_DONE: |
| 153 | cec_received_msg(cec->adap, &cec->msg); |
| 154 | cec->rx = STATE_IDLE; |
| 155 | break; |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | return IRQ_HANDLED; |
| 161 | } |
| 162 | |
| 163 | static const struct cec_adap_ops s5p_cec_adap_ops = { |
| 164 | .adap_enable = s5p_cec_adap_enable, |
| 165 | .adap_log_addr = s5p_cec_adap_log_addr, |
| 166 | .adap_transmit = s5p_cec_adap_transmit, |
| 167 | }; |
| 168 | |
| 169 | static int s5p_cec_probe(struct platform_device *pdev) |
| 170 | { |
| 171 | struct device *dev = &pdev->dev; |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 172 | struct device_node *np; |
| 173 | struct platform_device *hdmi_dev; |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 174 | struct resource *res; |
| 175 | struct s5p_cec_dev *cec; |
Hans Verkuil | d070394 | 2017-06-07 11:46:15 -0300 | [diff] [blame] | 176 | bool needs_hpd = of_property_read_bool(pdev->dev.of_node, "needs-hpd"); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 177 | int ret; |
| 178 | |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 179 | np = of_parse_phandle(pdev->dev.of_node, "hdmi-phandle", 0); |
| 180 | |
| 181 | if (!np) { |
| 182 | dev_err(&pdev->dev, "Failed to find hdmi node in device tree\n"); |
| 183 | return -ENODEV; |
| 184 | } |
| 185 | hdmi_dev = of_find_device_by_node(np); |
| 186 | if (hdmi_dev == NULL) |
| 187 | return -EPROBE_DEFER; |
| 188 | |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 189 | cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL); |
Christophe JAILLET | 6e7122a | 2016-08-31 10:45:04 -0300 | [diff] [blame] | 190 | if (!cec) |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 191 | return -ENOMEM; |
| 192 | |
| 193 | cec->dev = dev; |
| 194 | |
| 195 | cec->irq = platform_get_irq(pdev, 0); |
| 196 | if (cec->irq < 0) |
| 197 | return cec->irq; |
| 198 | |
| 199 | ret = devm_request_threaded_irq(dev, cec->irq, s5p_cec_irq_handler, |
| 200 | s5p_cec_irq_handler_thread, 0, pdev->name, cec); |
| 201 | if (ret) |
| 202 | return ret; |
| 203 | |
| 204 | cec->clk = devm_clk_get(dev, "hdmicec"); |
| 205 | if (IS_ERR(cec->clk)) |
| 206 | return PTR_ERR(cec->clk); |
| 207 | |
| 208 | cec->pmu = syscon_regmap_lookup_by_phandle(dev->of_node, |
| 209 | "samsung,syscon-phandle"); |
| 210 | if (IS_ERR(cec->pmu)) |
| 211 | return -EPROBE_DEFER; |
| 212 | |
| 213 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 214 | cec->reg = devm_ioremap_resource(dev, res); |
| 215 | if (IS_ERR(cec->reg)) |
| 216 | return PTR_ERR(cec->reg); |
| 217 | |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 218 | cec->notifier = cec_notifier_get(&hdmi_dev->dev); |
| 219 | if (cec->notifier == NULL) |
| 220 | return -ENOMEM; |
| 221 | |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 222 | cec->adap = cec_allocate_adapter(&s5p_cec_adap_ops, cec, |
| 223 | CEC_NAME, |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 224 | CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT | |
Hans Verkuil | d070394 | 2017-06-07 11:46:15 -0300 | [diff] [blame] | 225 | CEC_CAP_PASSTHROUGH | CEC_CAP_RC | |
| 226 | (needs_hpd ? CEC_CAP_NEEDS_HPD : 0), 1); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 227 | ret = PTR_ERR_OR_ZERO(cec->adap); |
| 228 | if (ret) |
| 229 | return ret; |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 230 | |
Hans Verkuil | f51e808 | 2016-11-25 06:23:34 -0200 | [diff] [blame] | 231 | ret = cec_register_adapter(cec->adap, &pdev->dev); |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 232 | if (ret) |
| 233 | goto err_delete_adapter; |
| 234 | |
| 235 | cec_register_cec_notifier(cec->adap, cec->notifier); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 236 | |
| 237 | platform_set_drvdata(pdev, cec); |
| 238 | pm_runtime_enable(dev); |
| 239 | |
Rene Hickersberger | ac5c03b | 2017-05-05 11:09:58 -0300 | [diff] [blame] | 240 | dev_dbg(dev, "successfully probed\n"); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 241 | return 0; |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 242 | |
| 243 | err_delete_adapter: |
| 244 | cec_delete_adapter(cec->adap); |
| 245 | return ret; |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static int s5p_cec_remove(struct platform_device *pdev) |
| 249 | { |
| 250 | struct s5p_cec_dev *cec = platform_get_drvdata(pdev); |
| 251 | |
| 252 | cec_unregister_adapter(cec->adap); |
Hans Verkuil | a93d429 | 2016-12-13 12:37:16 -0200 | [diff] [blame] | 253 | cec_notifier_put(cec->notifier); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 254 | pm_runtime_disable(&pdev->dev); |
| 255 | return 0; |
| 256 | } |
| 257 | |
Arnd Bergmann | eadf081 | 2016-10-13 11:39:04 -0300 | [diff] [blame] | 258 | static int __maybe_unused s5p_cec_runtime_suspend(struct device *dev) |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 259 | { |
| 260 | struct s5p_cec_dev *cec = dev_get_drvdata(dev); |
| 261 | |
| 262 | clk_disable_unprepare(cec->clk); |
| 263 | return 0; |
| 264 | } |
| 265 | |
Arnd Bergmann | eadf081 | 2016-10-13 11:39:04 -0300 | [diff] [blame] | 266 | static int __maybe_unused s5p_cec_runtime_resume(struct device *dev) |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 267 | { |
| 268 | struct s5p_cec_dev *cec = dev_get_drvdata(dev); |
| 269 | int ret; |
| 270 | |
| 271 | ret = clk_prepare_enable(cec->clk); |
| 272 | if (ret < 0) |
| 273 | return ret; |
| 274 | return 0; |
| 275 | } |
| 276 | |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 277 | static const struct dev_pm_ops s5p_cec_pm_ops = { |
Marek Szyprowski | 57b978a | 2016-08-31 09:55:58 -0300 | [diff] [blame] | 278 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 279 | pm_runtime_force_resume) |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 280 | SET_RUNTIME_PM_OPS(s5p_cec_runtime_suspend, s5p_cec_runtime_resume, |
| 281 | NULL) |
| 282 | }; |
| 283 | |
| 284 | static const struct of_device_id s5p_cec_match[] = { |
| 285 | { |
| 286 | .compatible = "samsung,s5p-cec", |
| 287 | }, |
| 288 | {}, |
| 289 | }; |
Javier Martinez Canillas | 0dc5fb0 | 2016-10-17 13:44:11 -0200 | [diff] [blame] | 290 | MODULE_DEVICE_TABLE(of, s5p_cec_match); |
Kamil Debski | 1bcbf6f | 2015-09-07 08:15:27 -0300 | [diff] [blame] | 291 | |
| 292 | static struct platform_driver s5p_cec_pdrv = { |
| 293 | .probe = s5p_cec_probe, |
| 294 | .remove = s5p_cec_remove, |
| 295 | .driver = { |
| 296 | .name = CEC_NAME, |
| 297 | .of_match_table = s5p_cec_match, |
| 298 | .pm = &s5p_cec_pm_ops, |
| 299 | }, |
| 300 | }; |
| 301 | |
| 302 | module_platform_driver(s5p_cec_pdrv); |
| 303 | |
| 304 | MODULE_AUTHOR("Kamil Debski <kamil@wypas.org>"); |
| 305 | MODULE_LICENSE("GPL"); |
| 306 | MODULE_DESCRIPTION("Samsung S5P CEC driver"); |