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