ao-cec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * Driver for Amlogic Meson AO CEC Controller
  3. *
  4. * Copyright (C) 2015 Amlogic, Inc. All rights reserved
  5. * Copyright (C) 2017 BayLibre, SAS
  6. * Author: Neil Armstrong <narmstrong@baylibre.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <linux/bitfield.h>
  11. #include <linux/clk.h>
  12. #include <linux/device.h>
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/kernel.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/types.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/reset.h>
  23. #include <media/cec.h>
  24. #include <media/cec-notifier.h>
  25. /* CEC Registers */
  26. /*
  27. * [2:1] cntl_clk
  28. * - 0 = Disable clk (Power-off mode)
  29. * - 1 = Enable gated clock (Normal mode)
  30. * - 2 = Enable free-run clk (Debug mode)
  31. */
  32. #define CEC_GEN_CNTL_REG 0x00
  33. #define CEC_GEN_CNTL_RESET BIT(0)
  34. #define CEC_GEN_CNTL_CLK_DISABLE 0
  35. #define CEC_GEN_CNTL_CLK_ENABLE 1
  36. #define CEC_GEN_CNTL_CLK_ENABLE_DBG 2
  37. #define CEC_GEN_CNTL_CLK_CTRL_MASK GENMASK(2, 1)
  38. /*
  39. * [7:0] cec_reg_addr
  40. * [15:8] cec_reg_wrdata
  41. * [16] cec_reg_wr
  42. * - 0 = Read
  43. * - 1 = Write
  44. * [23] bus free
  45. * [31:24] cec_reg_rddata
  46. */
  47. #define CEC_RW_REG 0x04
  48. #define CEC_RW_ADDR GENMASK(7, 0)
  49. #define CEC_RW_WR_DATA GENMASK(15, 8)
  50. #define CEC_RW_WRITE_EN BIT(16)
  51. #define CEC_RW_BUS_BUSY BIT(23)
  52. #define CEC_RW_RD_DATA GENMASK(31, 24)
  53. /*
  54. * [1] tx intr
  55. * [2] rx intr
  56. */
  57. #define CEC_INTR_MASKN_REG 0x08
  58. #define CEC_INTR_CLR_REG 0x0c
  59. #define CEC_INTR_STAT_REG 0x10
  60. #define CEC_INTR_TX BIT(1)
  61. #define CEC_INTR_RX BIT(2)
  62. /* CEC Commands */
  63. #define CEC_TX_MSG_0_HEADER 0x00
  64. #define CEC_TX_MSG_1_OPCODE 0x01
  65. #define CEC_TX_MSG_2_OP1 0x02
  66. #define CEC_TX_MSG_3_OP2 0x03
  67. #define CEC_TX_MSG_4_OP3 0x04
  68. #define CEC_TX_MSG_5_OP4 0x05
  69. #define CEC_TX_MSG_6_OP5 0x06
  70. #define CEC_TX_MSG_7_OP6 0x07
  71. #define CEC_TX_MSG_8_OP7 0x08
  72. #define CEC_TX_MSG_9_OP8 0x09
  73. #define CEC_TX_MSG_A_OP9 0x0A
  74. #define CEC_TX_MSG_B_OP10 0x0B
  75. #define CEC_TX_MSG_C_OP11 0x0C
  76. #define CEC_TX_MSG_D_OP12 0x0D
  77. #define CEC_TX_MSG_E_OP13 0x0E
  78. #define CEC_TX_MSG_F_OP14 0x0F
  79. #define CEC_TX_MSG_LENGTH 0x10
  80. #define CEC_TX_MSG_CMD 0x11
  81. #define CEC_TX_WRITE_BUF 0x12
  82. #define CEC_TX_CLEAR_BUF 0x13
  83. #define CEC_RX_MSG_CMD 0x14
  84. #define CEC_RX_CLEAR_BUF 0x15
  85. #define CEC_LOGICAL_ADDR0 0x16
  86. #define CEC_LOGICAL_ADDR1 0x17
  87. #define CEC_LOGICAL_ADDR2 0x18
  88. #define CEC_LOGICAL_ADDR3 0x19
  89. #define CEC_LOGICAL_ADDR4 0x1A
  90. #define CEC_CLOCK_DIV_H 0x1B
  91. #define CEC_CLOCK_DIV_L 0x1C
  92. #define CEC_QUIESCENT_25MS_BIT7_0 0x20
  93. #define CEC_QUIESCENT_25MS_BIT11_8 0x21
  94. #define CEC_STARTBITMINL2H_3MS5_BIT7_0 0x22
  95. #define CEC_STARTBITMINL2H_3MS5_BIT8 0x23
  96. #define CEC_STARTBITMAXL2H_3MS9_BIT7_0 0x24
  97. #define CEC_STARTBITMAXL2H_3MS9_BIT8 0x25
  98. #define CEC_STARTBITMINH_0MS6_BIT7_0 0x26
  99. #define CEC_STARTBITMINH_0MS6_BIT8 0x27
  100. #define CEC_STARTBITMAXH_1MS0_BIT7_0 0x28
  101. #define CEC_STARTBITMAXH_1MS0_BIT8 0x29
  102. #define CEC_STARTBITMINTOT_4MS3_BIT7_0 0x2A
  103. #define CEC_STARTBITMINTOT_4MS3_BIT9_8 0x2B
  104. #define CEC_STARTBITMAXTOT_4MS7_BIT7_0 0x2C
  105. #define CEC_STARTBITMAXTOT_4MS7_BIT9_8 0x2D
  106. #define CEC_LOGIC1MINL2H_0MS4_BIT7_0 0x2E
  107. #define CEC_LOGIC1MINL2H_0MS4_BIT8 0x2F
  108. #define CEC_LOGIC1MAXL2H_0MS8_BIT7_0 0x30
  109. #define CEC_LOGIC1MAXL2H_0MS8_BIT8 0x31
  110. #define CEC_LOGIC0MINL2H_1MS3_BIT7_0 0x32
  111. #define CEC_LOGIC0MINL2H_1MS3_BIT8 0x33
  112. #define CEC_LOGIC0MAXL2H_1MS7_BIT7_0 0x34
  113. #define CEC_LOGIC0MAXL2H_1MS7_BIT8 0x35
  114. #define CEC_LOGICMINTOTAL_2MS05_BIT7_0 0x36
  115. #define CEC_LOGICMINTOTAL_2MS05_BIT9_8 0x37
  116. #define CEC_LOGICMAXHIGH_2MS8_BIT7_0 0x38
  117. #define CEC_LOGICMAXHIGH_2MS8_BIT8 0x39
  118. #define CEC_LOGICERRLOW_3MS4_BIT7_0 0x3A
  119. #define CEC_LOGICERRLOW_3MS4_BIT8 0x3B
  120. #define CEC_NOMSMPPOINT_1MS05 0x3C
  121. #define CEC_DELCNTR_LOGICERR 0x3E
  122. #define CEC_TXTIME_17MS_BIT7_0 0x40
  123. #define CEC_TXTIME_17MS_BIT10_8 0x41
  124. #define CEC_TXTIME_2BIT_BIT7_0 0x42
  125. #define CEC_TXTIME_2BIT_BIT10_8 0x43
  126. #define CEC_TXTIME_4BIT_BIT7_0 0x44
  127. #define CEC_TXTIME_4BIT_BIT10_8 0x45
  128. #define CEC_STARTBITNOML2H_3MS7_BIT7_0 0x46
  129. #define CEC_STARTBITNOML2H_3MS7_BIT8 0x47
  130. #define CEC_STARTBITNOMH_0MS8_BIT7_0 0x48
  131. #define CEC_STARTBITNOMH_0MS8_BIT8 0x49
  132. #define CEC_LOGIC1NOML2H_0MS6_BIT7_0 0x4A
  133. #define CEC_LOGIC1NOML2H_0MS6_BIT8 0x4B
  134. #define CEC_LOGIC0NOML2H_1MS5_BIT7_0 0x4C
  135. #define CEC_LOGIC0NOML2H_1MS5_BIT8 0x4D
  136. #define CEC_LOGIC1NOMH_1MS8_BIT7_0 0x4E
  137. #define CEC_LOGIC1NOMH_1MS8_BIT8 0x4F
  138. #define CEC_LOGIC0NOMH_0MS9_BIT7_0 0x50
  139. #define CEC_LOGIC0NOMH_0MS9_BIT8 0x51
  140. #define CEC_LOGICERRLOW_3MS6_BIT7_0 0x52
  141. #define CEC_LOGICERRLOW_3MS6_BIT8 0x53
  142. #define CEC_CHKCONTENTION_0MS1 0x54
  143. #define CEC_PREPARENXTBIT_0MS05_BIT7_0 0x56
  144. #define CEC_PREPARENXTBIT_0MS05_BIT8 0x57
  145. #define CEC_NOMSMPACKPOINT_0MS45 0x58
  146. #define CEC_ACK0NOML2H_1MS5_BIT7_0 0x5A
  147. #define CEC_ACK0NOML2H_1MS5_BIT8 0x5B
  148. #define CEC_BUGFIX_DISABLE_0 0x60
  149. #define CEC_BUGFIX_DISABLE_1 0x61
  150. #define CEC_RX_MSG_0_HEADER 0x80
  151. #define CEC_RX_MSG_1_OPCODE 0x81
  152. #define CEC_RX_MSG_2_OP1 0x82
  153. #define CEC_RX_MSG_3_OP2 0x83
  154. #define CEC_RX_MSG_4_OP3 0x84
  155. #define CEC_RX_MSG_5_OP4 0x85
  156. #define CEC_RX_MSG_6_OP5 0x86
  157. #define CEC_RX_MSG_7_OP6 0x87
  158. #define CEC_RX_MSG_8_OP7 0x88
  159. #define CEC_RX_MSG_9_OP8 0x89
  160. #define CEC_RX_MSG_A_OP9 0x8A
  161. #define CEC_RX_MSG_B_OP10 0x8B
  162. #define CEC_RX_MSG_C_OP11 0x8C
  163. #define CEC_RX_MSG_D_OP12 0x8D
  164. #define CEC_RX_MSG_E_OP13 0x8E
  165. #define CEC_RX_MSG_F_OP14 0x8F
  166. #define CEC_RX_MSG_LENGTH 0x90
  167. #define CEC_RX_MSG_STATUS 0x91
  168. #define CEC_RX_NUM_MSG 0x92
  169. #define CEC_TX_MSG_STATUS 0x93
  170. #define CEC_TX_NUM_MSG 0x94
  171. /* CEC_TX_MSG_CMD definition */
  172. #define TX_NO_OP 0 /* No transaction */
  173. #define TX_REQ_CURRENT 1 /* Transmit earliest message in buffer */
  174. #define TX_ABORT 2 /* Abort transmitting earliest message */
  175. #define TX_REQ_NEXT 3 /* Overwrite earliest msg, transmit next */
  176. /* tx_msg_status definition */
  177. #define TX_IDLE 0 /* No transaction */
  178. #define TX_BUSY 1 /* Transmitter is busy */
  179. #define TX_DONE 2 /* Message successfully transmitted */
  180. #define TX_ERROR 3 /* Message transmitted with error */
  181. /* rx_msg_cmd */
  182. #define RX_NO_OP 0 /* No transaction */
  183. #define RX_ACK_CURRENT 1 /* Read earliest message in buffer */
  184. #define RX_DISABLE 2 /* Disable receiving latest message */
  185. #define RX_ACK_NEXT 3 /* Clear earliest msg, read next */
  186. /* rx_msg_status */
  187. #define RX_IDLE 0 /* No transaction */
  188. #define RX_BUSY 1 /* Receiver is busy */
  189. #define RX_DONE 2 /* Message has been received successfully */
  190. #define RX_ERROR 3 /* Message has been received with error */
  191. /* RX_CLEAR_BUF options */
  192. #define CLEAR_START 1
  193. #define CLEAR_STOP 0
  194. /* CEC_LOGICAL_ADDRx options */
  195. #define LOGICAL_ADDR_MASK 0xf
  196. #define LOGICAL_ADDR_VALID BIT(4)
  197. #define LOGICAL_ADDR_DISABLE 0
  198. #define CEC_CLK_RATE 32768
  199. struct meson_ao_cec_device {
  200. struct platform_device *pdev;
  201. void __iomem *base;
  202. struct clk *core;
  203. spinlock_t cec_reg_lock;
  204. struct cec_notifier *notify;
  205. struct cec_adapter *adap;
  206. struct cec_msg rx_msg;
  207. };
  208. #define writel_bits_relaxed(mask, val, addr) \
  209. writel_relaxed((readl_relaxed(addr) & ~(mask)) | (val), addr)
  210. static inline int meson_ao_cec_wait_busy(struct meson_ao_cec_device *ao_cec)
  211. {
  212. ktime_t timeout = ktime_add_us(ktime_get(), 5000);
  213. while (readl_relaxed(ao_cec->base + CEC_RW_REG) & CEC_RW_BUS_BUSY) {
  214. if (ktime_compare(ktime_get(), timeout) > 0)
  215. return -ETIMEDOUT;
  216. }
  217. return 0;
  218. }
  219. static void meson_ao_cec_read(struct meson_ao_cec_device *ao_cec,
  220. unsigned long address, u8 *data,
  221. int *res)
  222. {
  223. unsigned long flags;
  224. u32 reg = FIELD_PREP(CEC_RW_ADDR, address);
  225. int ret = 0;
  226. if (res && *res)
  227. return;
  228. spin_lock_irqsave(&ao_cec->cec_reg_lock, flags);
  229. ret = meson_ao_cec_wait_busy(ao_cec);
  230. if (ret)
  231. goto read_out;
  232. writel_relaxed(reg, ao_cec->base + CEC_RW_REG);
  233. ret = meson_ao_cec_wait_busy(ao_cec);
  234. if (ret)
  235. goto read_out;
  236. *data = FIELD_GET(CEC_RW_RD_DATA,
  237. readl_relaxed(ao_cec->base + CEC_RW_REG));
  238. read_out:
  239. spin_unlock_irqrestore(&ao_cec->cec_reg_lock, flags);
  240. if (res)
  241. *res = ret;
  242. }
  243. static void meson_ao_cec_write(struct meson_ao_cec_device *ao_cec,
  244. unsigned long address, u8 data,
  245. int *res)
  246. {
  247. unsigned long flags;
  248. u32 reg = FIELD_PREP(CEC_RW_ADDR, address) |
  249. FIELD_PREP(CEC_RW_WR_DATA, data) |
  250. CEC_RW_WRITE_EN;
  251. int ret = 0;
  252. if (res && *res)
  253. return;
  254. spin_lock_irqsave(&ao_cec->cec_reg_lock, flags);
  255. ret = meson_ao_cec_wait_busy(ao_cec);
  256. if (ret)
  257. goto write_out;
  258. writel_relaxed(reg, ao_cec->base + CEC_RW_REG);
  259. write_out:
  260. spin_unlock_irqrestore(&ao_cec->cec_reg_lock, flags);
  261. if (res)
  262. *res = ret;
  263. }
  264. static inline void meson_ao_cec_irq_setup(struct meson_ao_cec_device *ao_cec,
  265. bool enable)
  266. {
  267. u32 cfg = CEC_INTR_TX | CEC_INTR_RX;
  268. writel_bits_relaxed(cfg, enable ? cfg : 0,
  269. ao_cec->base + CEC_INTR_MASKN_REG);
  270. }
  271. static inline int meson_ao_cec_clear(struct meson_ao_cec_device *ao_cec)
  272. {
  273. int ret = 0;
  274. meson_ao_cec_write(ao_cec, CEC_RX_MSG_CMD, RX_DISABLE, &ret);
  275. meson_ao_cec_write(ao_cec, CEC_TX_MSG_CMD, TX_ABORT, &ret);
  276. meson_ao_cec_write(ao_cec, CEC_RX_CLEAR_BUF, 1, &ret);
  277. meson_ao_cec_write(ao_cec, CEC_TX_CLEAR_BUF, 1, &ret);
  278. if (ret)
  279. return ret;
  280. udelay(100);
  281. meson_ao_cec_write(ao_cec, CEC_RX_CLEAR_BUF, 0, &ret);
  282. meson_ao_cec_write(ao_cec, CEC_TX_CLEAR_BUF, 0, &ret);
  283. if (ret)
  284. return ret;
  285. udelay(100);
  286. meson_ao_cec_write(ao_cec, CEC_RX_MSG_CMD, RX_NO_OP, &ret);
  287. meson_ao_cec_write(ao_cec, CEC_TX_MSG_CMD, TX_NO_OP, &ret);
  288. return ret;
  289. }
  290. static int meson_ao_cec_arbit_bit_time_set(struct meson_ao_cec_device *ao_cec,
  291. unsigned int bit_set,
  292. unsigned int time_set)
  293. {
  294. int ret = 0;
  295. switch (bit_set) {
  296. case CEC_SIGNAL_FREE_TIME_RETRY:
  297. meson_ao_cec_write(ao_cec, CEC_TXTIME_4BIT_BIT7_0,
  298. time_set & 0xff, &ret);
  299. meson_ao_cec_write(ao_cec, CEC_TXTIME_4BIT_BIT10_8,
  300. (time_set >> 8) & 0x7, &ret);
  301. break;
  302. case CEC_SIGNAL_FREE_TIME_NEW_INITIATOR:
  303. meson_ao_cec_write(ao_cec, CEC_TXTIME_2BIT_BIT7_0,
  304. time_set & 0xff, &ret);
  305. meson_ao_cec_write(ao_cec, CEC_TXTIME_2BIT_BIT10_8,
  306. (time_set >> 8) & 0x7, &ret);
  307. break;
  308. case CEC_SIGNAL_FREE_TIME_NEXT_XFER:
  309. meson_ao_cec_write(ao_cec, CEC_TXTIME_17MS_BIT7_0,
  310. time_set & 0xff, &ret);
  311. meson_ao_cec_write(ao_cec, CEC_TXTIME_17MS_BIT10_8,
  312. (time_set >> 8) & 0x7, &ret);
  313. break;
  314. }
  315. return ret;
  316. }
  317. static irqreturn_t meson_ao_cec_irq(int irq, void *data)
  318. {
  319. struct meson_ao_cec_device *ao_cec = data;
  320. u32 stat = readl_relaxed(ao_cec->base + CEC_INTR_STAT_REG);
  321. if (stat)
  322. return IRQ_WAKE_THREAD;
  323. return IRQ_NONE;
  324. }
  325. static void meson_ao_cec_irq_tx(struct meson_ao_cec_device *ao_cec)
  326. {
  327. unsigned long tx_status = 0;
  328. u8 stat;
  329. int ret = 0;
  330. meson_ao_cec_read(ao_cec, CEC_TX_MSG_STATUS, &stat, &ret);
  331. if (ret)
  332. goto tx_reg_err;
  333. switch (stat) {
  334. case TX_DONE:
  335. tx_status = CEC_TX_STATUS_OK;
  336. break;
  337. case TX_BUSY:
  338. tx_status = CEC_TX_STATUS_ARB_LOST;
  339. break;
  340. case TX_IDLE:
  341. tx_status = CEC_TX_STATUS_LOW_DRIVE;
  342. break;
  343. case TX_ERROR:
  344. default:
  345. tx_status = CEC_TX_STATUS_NACK;
  346. break;
  347. }
  348. /* Clear Interruption */
  349. writel_relaxed(CEC_INTR_TX, ao_cec->base + CEC_INTR_CLR_REG);
  350. /* Stop TX */
  351. meson_ao_cec_write(ao_cec, CEC_TX_MSG_CMD, TX_NO_OP, &ret);
  352. if (ret)
  353. goto tx_reg_err;
  354. cec_transmit_attempt_done(ao_cec->adap, tx_status);
  355. return;
  356. tx_reg_err:
  357. cec_transmit_attempt_done(ao_cec->adap, CEC_TX_STATUS_ERROR);
  358. }
  359. static void meson_ao_cec_irq_rx(struct meson_ao_cec_device *ao_cec)
  360. {
  361. int i, ret = 0;
  362. u8 reg;
  363. meson_ao_cec_read(ao_cec, CEC_RX_MSG_STATUS, &reg, &ret);
  364. if (reg != RX_DONE)
  365. goto rx_out;
  366. meson_ao_cec_read(ao_cec, CEC_RX_NUM_MSG, &reg, &ret);
  367. if (reg != 1)
  368. goto rx_out;
  369. meson_ao_cec_read(ao_cec, CEC_RX_MSG_LENGTH, &reg, &ret);
  370. ao_cec->rx_msg.len = reg + 1;
  371. if (ao_cec->rx_msg.len > CEC_MAX_MSG_SIZE)
  372. ao_cec->rx_msg.len = CEC_MAX_MSG_SIZE;
  373. for (i = 0; i < ao_cec->rx_msg.len; i++) {
  374. u8 byte;
  375. meson_ao_cec_read(ao_cec, CEC_RX_MSG_0_HEADER + i, &byte, &ret);
  376. ao_cec->rx_msg.msg[i] = byte;
  377. }
  378. if (ret)
  379. goto rx_out;
  380. cec_received_msg(ao_cec->adap, &ao_cec->rx_msg);
  381. rx_out:
  382. /* Clear Interruption */
  383. writel_relaxed(CEC_INTR_RX, ao_cec->base + CEC_INTR_CLR_REG);
  384. /* Ack RX message */
  385. meson_ao_cec_write(ao_cec, CEC_RX_MSG_CMD, RX_ACK_CURRENT, &ret);
  386. meson_ao_cec_write(ao_cec, CEC_RX_MSG_CMD, RX_NO_OP, &ret);
  387. /* Clear RX buffer */
  388. meson_ao_cec_write(ao_cec, CEC_RX_CLEAR_BUF, CLEAR_START, &ret);
  389. meson_ao_cec_write(ao_cec, CEC_RX_CLEAR_BUF, CLEAR_STOP, &ret);
  390. }
  391. static irqreturn_t meson_ao_cec_irq_thread(int irq, void *data)
  392. {
  393. struct meson_ao_cec_device *ao_cec = data;
  394. u32 stat = readl_relaxed(ao_cec->base + CEC_INTR_STAT_REG);
  395. if (stat & CEC_INTR_TX)
  396. meson_ao_cec_irq_tx(ao_cec);
  397. meson_ao_cec_irq_rx(ao_cec);
  398. return IRQ_HANDLED;
  399. }
  400. static int meson_ao_cec_set_log_addr(struct cec_adapter *adap, u8 logical_addr)
  401. {
  402. struct meson_ao_cec_device *ao_cec = adap->priv;
  403. int ret = 0;
  404. meson_ao_cec_write(ao_cec, CEC_LOGICAL_ADDR0,
  405. LOGICAL_ADDR_DISABLE, &ret);
  406. if (ret)
  407. return ret;
  408. ret = meson_ao_cec_clear(ao_cec);
  409. if (ret)
  410. return ret;
  411. if (logical_addr == CEC_LOG_ADDR_INVALID)
  412. return 0;
  413. meson_ao_cec_write(ao_cec, CEC_LOGICAL_ADDR0,
  414. logical_addr & LOGICAL_ADDR_MASK, &ret);
  415. if (ret)
  416. return ret;
  417. udelay(100);
  418. meson_ao_cec_write(ao_cec, CEC_LOGICAL_ADDR0,
  419. (logical_addr & LOGICAL_ADDR_MASK) |
  420. LOGICAL_ADDR_VALID, &ret);
  421. return ret;
  422. }
  423. static int meson_ao_cec_transmit(struct cec_adapter *adap, u8 attempts,
  424. u32 signal_free_time, struct cec_msg *msg)
  425. {
  426. struct meson_ao_cec_device *ao_cec = adap->priv;
  427. int i, ret = 0;
  428. u8 reg;
  429. meson_ao_cec_read(ao_cec, CEC_TX_MSG_STATUS, &reg, &ret);
  430. if (ret)
  431. return ret;
  432. if (reg == TX_BUSY) {
  433. dev_err(&ao_cec->pdev->dev, "%s: busy TX: aborting\n",
  434. __func__);
  435. meson_ao_cec_write(ao_cec, CEC_TX_MSG_CMD, TX_ABORT, &ret);
  436. }
  437. for (i = 0; i < msg->len; i++) {
  438. meson_ao_cec_write(ao_cec, CEC_TX_MSG_0_HEADER + i,
  439. msg->msg[i], &ret);
  440. }
  441. meson_ao_cec_write(ao_cec, CEC_TX_MSG_LENGTH, msg->len - 1, &ret);
  442. meson_ao_cec_write(ao_cec, CEC_TX_MSG_CMD, TX_REQ_CURRENT, &ret);
  443. return ret;
  444. }
  445. static int meson_ao_cec_adap_enable(struct cec_adapter *adap, bool enable)
  446. {
  447. struct meson_ao_cec_device *ao_cec = adap->priv;
  448. int ret;
  449. meson_ao_cec_irq_setup(ao_cec, false);
  450. writel_bits_relaxed(CEC_GEN_CNTL_RESET, CEC_GEN_CNTL_RESET,
  451. ao_cec->base + CEC_GEN_CNTL_REG);
  452. if (!enable)
  453. return 0;
  454. /* Enable gated clock (Normal mode). */
  455. writel_bits_relaxed(CEC_GEN_CNTL_CLK_CTRL_MASK,
  456. FIELD_PREP(CEC_GEN_CNTL_CLK_CTRL_MASK,
  457. CEC_GEN_CNTL_CLK_ENABLE),
  458. ao_cec->base + CEC_GEN_CNTL_REG);
  459. udelay(100);
  460. /* Release Reset */
  461. writel_bits_relaxed(CEC_GEN_CNTL_RESET, 0,
  462. ao_cec->base + CEC_GEN_CNTL_REG);
  463. /* Clear buffers */
  464. ret = meson_ao_cec_clear(ao_cec);
  465. if (ret)
  466. return ret;
  467. /* CEC arbitration 3/5/7 bit time set. */
  468. ret = meson_ao_cec_arbit_bit_time_set(ao_cec,
  469. CEC_SIGNAL_FREE_TIME_RETRY,
  470. 0x118);
  471. if (ret)
  472. return ret;
  473. ret = meson_ao_cec_arbit_bit_time_set(ao_cec,
  474. CEC_SIGNAL_FREE_TIME_NEW_INITIATOR,
  475. 0x000);
  476. if (ret)
  477. return ret;
  478. ret = meson_ao_cec_arbit_bit_time_set(ao_cec,
  479. CEC_SIGNAL_FREE_TIME_NEXT_XFER,
  480. 0x2aa);
  481. if (ret)
  482. return ret;
  483. meson_ao_cec_irq_setup(ao_cec, true);
  484. return 0;
  485. }
  486. static const struct cec_adap_ops meson_ao_cec_ops = {
  487. .adap_enable = meson_ao_cec_adap_enable,
  488. .adap_log_addr = meson_ao_cec_set_log_addr,
  489. .adap_transmit = meson_ao_cec_transmit,
  490. };
  491. static int meson_ao_cec_probe(struct platform_device *pdev)
  492. {
  493. struct meson_ao_cec_device *ao_cec;
  494. struct platform_device *hdmi_dev;
  495. struct device_node *np;
  496. struct resource *res;
  497. int ret, irq;
  498. np = of_parse_phandle(pdev->dev.of_node, "hdmi-phandle", 0);
  499. if (!np) {
  500. dev_err(&pdev->dev, "Failed to find hdmi node\n");
  501. return -ENODEV;
  502. }
  503. hdmi_dev = of_find_device_by_node(np);
  504. if (hdmi_dev == NULL)
  505. return -EPROBE_DEFER;
  506. ao_cec = devm_kzalloc(&pdev->dev, sizeof(*ao_cec), GFP_KERNEL);
  507. if (!ao_cec)
  508. return -ENOMEM;
  509. spin_lock_init(&ao_cec->cec_reg_lock);
  510. ao_cec->notify = cec_notifier_get(&hdmi_dev->dev);
  511. if (!ao_cec->notify)
  512. return -ENOMEM;
  513. ao_cec->adap = cec_allocate_adapter(&meson_ao_cec_ops, ao_cec,
  514. "meson_ao_cec",
  515. CEC_CAP_LOG_ADDRS |
  516. CEC_CAP_TRANSMIT |
  517. CEC_CAP_RC |
  518. CEC_CAP_PASSTHROUGH,
  519. 1); /* Use 1 for now */
  520. if (IS_ERR(ao_cec->adap)) {
  521. ret = PTR_ERR(ao_cec->adap);
  522. goto out_probe_notify;
  523. }
  524. ao_cec->adap->owner = THIS_MODULE;
  525. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  526. ao_cec->base = devm_ioremap_resource(&pdev->dev, res);
  527. if (IS_ERR(ao_cec->base)) {
  528. ret = PTR_ERR(ao_cec->base);
  529. goto out_probe_adapter;
  530. }
  531. irq = platform_get_irq(pdev, 0);
  532. ret = devm_request_threaded_irq(&pdev->dev, irq,
  533. meson_ao_cec_irq,
  534. meson_ao_cec_irq_thread,
  535. 0, NULL, ao_cec);
  536. if (ret) {
  537. dev_err(&pdev->dev, "irq request failed\n");
  538. goto out_probe_adapter;
  539. }
  540. ao_cec->core = devm_clk_get(&pdev->dev, "core");
  541. if (IS_ERR(ao_cec->core)) {
  542. dev_err(&pdev->dev, "core clock request failed\n");
  543. ret = PTR_ERR(ao_cec->core);
  544. goto out_probe_adapter;
  545. }
  546. ret = clk_prepare_enable(ao_cec->core);
  547. if (ret) {
  548. dev_err(&pdev->dev, "core clock enable failed\n");
  549. goto out_probe_adapter;
  550. }
  551. ret = clk_set_rate(ao_cec->core, CEC_CLK_RATE);
  552. if (ret) {
  553. dev_err(&pdev->dev, "core clock set rate failed\n");
  554. goto out_probe_clk;
  555. }
  556. device_reset_optional(&pdev->dev);
  557. ao_cec->pdev = pdev;
  558. platform_set_drvdata(pdev, ao_cec);
  559. ret = cec_register_adapter(ao_cec->adap, &pdev->dev);
  560. if (ret < 0) {
  561. cec_notifier_put(ao_cec->notify);
  562. goto out_probe_clk;
  563. }
  564. /* Setup Hardware */
  565. writel_relaxed(CEC_GEN_CNTL_RESET,
  566. ao_cec->base + CEC_GEN_CNTL_REG);
  567. cec_register_cec_notifier(ao_cec->adap, ao_cec->notify);
  568. return 0;
  569. out_probe_clk:
  570. clk_disable_unprepare(ao_cec->core);
  571. out_probe_adapter:
  572. cec_delete_adapter(ao_cec->adap);
  573. out_probe_notify:
  574. cec_notifier_put(ao_cec->notify);
  575. dev_err(&pdev->dev, "CEC controller registration failed\n");
  576. return ret;
  577. }
  578. static int meson_ao_cec_remove(struct platform_device *pdev)
  579. {
  580. struct meson_ao_cec_device *ao_cec = platform_get_drvdata(pdev);
  581. clk_disable_unprepare(ao_cec->core);
  582. cec_unregister_adapter(ao_cec->adap);
  583. cec_notifier_put(ao_cec->notify);
  584. return 0;
  585. }
  586. static const struct of_device_id meson_ao_cec_of_match[] = {
  587. { .compatible = "amlogic,meson-gx-ao-cec", },
  588. { /* sentinel */ }
  589. };
  590. MODULE_DEVICE_TABLE(of, meson_ao_cec_of_match);
  591. static struct platform_driver meson_ao_cec_driver = {
  592. .probe = meson_ao_cec_probe,
  593. .remove = meson_ao_cec_remove,
  594. .driver = {
  595. .name = "meson-ao-cec",
  596. .of_match_table = of_match_ptr(meson_ao_cec_of_match),
  597. },
  598. };
  599. module_platform_driver(meson_ao_cec_driver);
  600. MODULE_DESCRIPTION("Meson AO CEC Controller driver");
  601. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  602. MODULE_LICENSE("GPL");