adv748x-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * Driver for Analog Devices ADV748X HDMI receiver with AFE
  3. *
  4. * Copyright (C) 2017 Renesas Electronics Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Authors:
  12. * Koji Matsuoka <koji.matsuoka.xm@renesas.com>
  13. * Niklas Söderlund <niklas.soderlund@ragnatech.se>
  14. * Kieran Bingham <kieran.bingham@ideasonboard.com>
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/i2c.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/of_graph.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. #include <linux/v4l2-dv-timings.h>
  25. #include <media/v4l2-ctrls.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/v4l2-dv-timings.h>
  28. #include <media/v4l2-ioctl.h>
  29. #include "adv748x.h"
  30. /* -----------------------------------------------------------------------------
  31. * Register manipulation
  32. */
  33. #define ADV748X_REGMAP_CONF(n) \
  34. { \
  35. .name = n, \
  36. .reg_bits = 8, \
  37. .val_bits = 8, \
  38. .max_register = 0xff, \
  39. .cache_type = REGCACHE_NONE, \
  40. }
  41. static const struct regmap_config adv748x_regmap_cnf[] = {
  42. ADV748X_REGMAP_CONF("io"),
  43. ADV748X_REGMAP_CONF("dpll"),
  44. ADV748X_REGMAP_CONF("cp"),
  45. ADV748X_REGMAP_CONF("hdmi"),
  46. ADV748X_REGMAP_CONF("edid"),
  47. ADV748X_REGMAP_CONF("repeater"),
  48. ADV748X_REGMAP_CONF("infoframe"),
  49. ADV748X_REGMAP_CONF("cbus"),
  50. ADV748X_REGMAP_CONF("cec"),
  51. ADV748X_REGMAP_CONF("sdp"),
  52. ADV748X_REGMAP_CONF("txa"),
  53. ADV748X_REGMAP_CONF("txb"),
  54. };
  55. static int adv748x_configure_regmap(struct adv748x_state *state, int region)
  56. {
  57. int err;
  58. if (!state->i2c_clients[region])
  59. return -ENODEV;
  60. state->regmap[region] =
  61. devm_regmap_init_i2c(state->i2c_clients[region],
  62. &adv748x_regmap_cnf[region]);
  63. if (IS_ERR(state->regmap[region])) {
  64. err = PTR_ERR(state->regmap[region]);
  65. adv_err(state,
  66. "Error initializing regmap %d with error %d\n",
  67. region, err);
  68. return -EINVAL;
  69. }
  70. return 0;
  71. }
  72. struct adv748x_register_map {
  73. const char *name;
  74. u8 default_addr;
  75. };
  76. static const struct adv748x_register_map adv748x_default_addresses[] = {
  77. [ADV748X_PAGE_IO] = { "main", 0x70 },
  78. [ADV748X_PAGE_DPLL] = { "dpll", 0x26 },
  79. [ADV748X_PAGE_CP] = { "cp", 0x22 },
  80. [ADV748X_PAGE_HDMI] = { "hdmi", 0x34 },
  81. [ADV748X_PAGE_EDID] = { "edid", 0x36 },
  82. [ADV748X_PAGE_REPEATER] = { "repeater", 0x32 },
  83. [ADV748X_PAGE_INFOFRAME] = { "infoframe", 0x31 },
  84. [ADV748X_PAGE_CBUS] = { "cbus", 0x30 },
  85. [ADV748X_PAGE_CEC] = { "cec", 0x41 },
  86. [ADV748X_PAGE_SDP] = { "sdp", 0x79 },
  87. [ADV748X_PAGE_TXB] = { "txb", 0x48 },
  88. [ADV748X_PAGE_TXA] = { "txa", 0x4a },
  89. };
  90. static int adv748x_read_check(struct adv748x_state *state,
  91. int client_page, u8 reg)
  92. {
  93. struct i2c_client *client = state->i2c_clients[client_page];
  94. int err;
  95. unsigned int val;
  96. err = regmap_read(state->regmap[client_page], reg, &val);
  97. if (err) {
  98. adv_err(state, "error reading %02x, %02x\n",
  99. client->addr, reg);
  100. return err;
  101. }
  102. return val;
  103. }
  104. int adv748x_read(struct adv748x_state *state, u8 page, u8 reg)
  105. {
  106. return adv748x_read_check(state, page, reg);
  107. }
  108. int adv748x_write(struct adv748x_state *state, u8 page, u8 reg, u8 value)
  109. {
  110. return regmap_write(state->regmap[page], reg, value);
  111. }
  112. /* adv748x_write_block(): Write raw data with a maximum of I2C_SMBUS_BLOCK_MAX
  113. * size to one or more registers.
  114. *
  115. * A value of zero will be returned on success, a negative errno will
  116. * be returned in error cases.
  117. */
  118. int adv748x_write_block(struct adv748x_state *state, int client_page,
  119. unsigned int init_reg, const void *val,
  120. size_t val_len)
  121. {
  122. struct regmap *regmap = state->regmap[client_page];
  123. if (val_len > I2C_SMBUS_BLOCK_MAX)
  124. val_len = I2C_SMBUS_BLOCK_MAX;
  125. return regmap_raw_write(regmap, init_reg, val, val_len);
  126. }
  127. static int adv748x_set_slave_addresses(struct adv748x_state *state)
  128. {
  129. struct i2c_client *client;
  130. unsigned int i;
  131. u8 io_reg;
  132. for (i = ADV748X_PAGE_DPLL; i < ADV748X_PAGE_MAX; ++i) {
  133. io_reg = ADV748X_IO_SLAVE_ADDR_BASE + i;
  134. client = state->i2c_clients[i];
  135. io_write(state, io_reg, client->addr << 1);
  136. }
  137. return 0;
  138. }
  139. static void adv748x_unregister_clients(struct adv748x_state *state)
  140. {
  141. unsigned int i;
  142. for (i = 1; i < ARRAY_SIZE(state->i2c_clients); ++i)
  143. i2c_unregister_device(state->i2c_clients[i]);
  144. }
  145. static int adv748x_initialise_clients(struct adv748x_state *state)
  146. {
  147. unsigned int i;
  148. int ret;
  149. for (i = ADV748X_PAGE_DPLL; i < ADV748X_PAGE_MAX; ++i) {
  150. state->i2c_clients[i] = i2c_new_secondary_device(
  151. state->client,
  152. adv748x_default_addresses[i].name,
  153. adv748x_default_addresses[i].default_addr);
  154. if (state->i2c_clients[i] == NULL) {
  155. adv_err(state, "failed to create i2c client %u\n", i);
  156. return -ENOMEM;
  157. }
  158. ret = adv748x_configure_regmap(state, i);
  159. if (ret)
  160. return ret;
  161. }
  162. return adv748x_set_slave_addresses(state);
  163. }
  164. /**
  165. * struct adv748x_reg_value - Register write instruction
  166. * @page: Regmap page identifier
  167. * @reg: I2C register
  168. * @value: value to write to @page at @reg
  169. */
  170. struct adv748x_reg_value {
  171. u8 page;
  172. u8 reg;
  173. u8 value;
  174. };
  175. static int adv748x_write_regs(struct adv748x_state *state,
  176. const struct adv748x_reg_value *regs)
  177. {
  178. int ret;
  179. while (regs->page != ADV748X_PAGE_EOR) {
  180. if (regs->page == ADV748X_PAGE_WAIT) {
  181. msleep(regs->value);
  182. } else {
  183. ret = adv748x_write(state, regs->page, regs->reg,
  184. regs->value);
  185. if (ret < 0) {
  186. adv_err(state,
  187. "Error regs page: 0x%02x reg: 0x%02x\n",
  188. regs->page, regs->reg);
  189. return ret;
  190. }
  191. }
  192. regs++;
  193. }
  194. return 0;
  195. }
  196. /* -----------------------------------------------------------------------------
  197. * TXA and TXB
  198. */
  199. static const struct adv748x_reg_value adv748x_power_up_txa_4lane[] = {
  200. {ADV748X_PAGE_TXA, 0x00, 0x84}, /* Enable 4-lane MIPI */
  201. {ADV748X_PAGE_TXA, 0x00, 0xa4}, /* Set Auto DPHY Timing */
  202. {ADV748X_PAGE_TXA, 0x31, 0x82}, /* ADI Required Write */
  203. {ADV748X_PAGE_TXA, 0x1e, 0x40}, /* ADI Required Write */
  204. {ADV748X_PAGE_TXA, 0xda, 0x01}, /* i2c_mipi_pll_en - 1'b1 */
  205. {ADV748X_PAGE_WAIT, 0x00, 0x02},/* delay 2 */
  206. {ADV748X_PAGE_TXA, 0x00, 0x24 },/* Power-up CSI-TX */
  207. {ADV748X_PAGE_WAIT, 0x00, 0x01},/* delay 1 */
  208. {ADV748X_PAGE_TXA, 0xc1, 0x2b}, /* ADI Required Write */
  209. {ADV748X_PAGE_WAIT, 0x00, 0x01},/* delay 1 */
  210. {ADV748X_PAGE_TXA, 0x31, 0x80}, /* ADI Required Write */
  211. {ADV748X_PAGE_EOR, 0xff, 0xff} /* End of register table */
  212. };
  213. static const struct adv748x_reg_value adv748x_power_down_txa_4lane[] = {
  214. {ADV748X_PAGE_TXA, 0x31, 0x82}, /* ADI Required Write */
  215. {ADV748X_PAGE_TXA, 0x1e, 0x00}, /* ADI Required Write */
  216. {ADV748X_PAGE_TXA, 0x00, 0x84}, /* Enable 4-lane MIPI */
  217. {ADV748X_PAGE_TXA, 0xda, 0x01}, /* i2c_mipi_pll_en - 1'b1 */
  218. {ADV748X_PAGE_TXA, 0xc1, 0x3b}, /* ADI Required Write */
  219. {ADV748X_PAGE_EOR, 0xff, 0xff} /* End of register table */
  220. };
  221. static const struct adv748x_reg_value adv748x_power_up_txb_1lane[] = {
  222. {ADV748X_PAGE_TXB, 0x00, 0x81}, /* Enable 1-lane MIPI */
  223. {ADV748X_PAGE_TXB, 0x00, 0xa1}, /* Set Auto DPHY Timing */
  224. {ADV748X_PAGE_TXB, 0x31, 0x82}, /* ADI Required Write */
  225. {ADV748X_PAGE_TXB, 0x1e, 0x40}, /* ADI Required Write */
  226. {ADV748X_PAGE_TXB, 0xda, 0x01}, /* i2c_mipi_pll_en - 1'b1 */
  227. {ADV748X_PAGE_WAIT, 0x00, 0x02},/* delay 2 */
  228. {ADV748X_PAGE_TXB, 0x00, 0x21 },/* Power-up CSI-TX */
  229. {ADV748X_PAGE_WAIT, 0x00, 0x01},/* delay 1 */
  230. {ADV748X_PAGE_TXB, 0xc1, 0x2b}, /* ADI Required Write */
  231. {ADV748X_PAGE_WAIT, 0x00, 0x01},/* delay 1 */
  232. {ADV748X_PAGE_TXB, 0x31, 0x80}, /* ADI Required Write */
  233. {ADV748X_PAGE_EOR, 0xff, 0xff} /* End of register table */
  234. };
  235. static const struct adv748x_reg_value adv748x_power_down_txb_1lane[] = {
  236. {ADV748X_PAGE_TXB, 0x31, 0x82}, /* ADI Required Write */
  237. {ADV748X_PAGE_TXB, 0x1e, 0x00}, /* ADI Required Write */
  238. {ADV748X_PAGE_TXB, 0x00, 0x81}, /* Enable 4-lane MIPI */
  239. {ADV748X_PAGE_TXB, 0xda, 0x01}, /* i2c_mipi_pll_en - 1'b1 */
  240. {ADV748X_PAGE_TXB, 0xc1, 0x3b}, /* ADI Required Write */
  241. {ADV748X_PAGE_EOR, 0xff, 0xff} /* End of register table */
  242. };
  243. int adv748x_txa_power(struct adv748x_state *state, bool on)
  244. {
  245. int val;
  246. val = txa_read(state, ADV748X_CSI_FS_AS_LS);
  247. if (val < 0)
  248. return val;
  249. /*
  250. * This test against BIT(6) is not documented by the datasheet, but was
  251. * specified in the downstream driver.
  252. * Track with a WARN_ONCE to determine if it is ever set by HW.
  253. */
  254. WARN_ONCE((on && val & ADV748X_CSI_FS_AS_LS_UNKNOWN),
  255. "Enabling with unknown bit set");
  256. if (on)
  257. return adv748x_write_regs(state, adv748x_power_up_txa_4lane);
  258. return adv748x_write_regs(state, adv748x_power_down_txa_4lane);
  259. }
  260. int adv748x_txb_power(struct adv748x_state *state, bool on)
  261. {
  262. int val;
  263. val = txb_read(state, ADV748X_CSI_FS_AS_LS);
  264. if (val < 0)
  265. return val;
  266. /*
  267. * This test against BIT(6) is not documented by the datasheet, but was
  268. * specified in the downstream driver.
  269. * Track with a WARN_ONCE to determine if it is ever set by HW.
  270. */
  271. WARN_ONCE((on && val & ADV748X_CSI_FS_AS_LS_UNKNOWN),
  272. "Enabling with unknown bit set");
  273. if (on)
  274. return adv748x_write_regs(state, adv748x_power_up_txb_1lane);
  275. return adv748x_write_regs(state, adv748x_power_down_txb_1lane);
  276. }
  277. /* -----------------------------------------------------------------------------
  278. * Media Operations
  279. */
  280. static const struct media_entity_operations adv748x_media_ops = {
  281. .link_validate = v4l2_subdev_link_validate,
  282. };
  283. /* -----------------------------------------------------------------------------
  284. * HW setup
  285. */
  286. static const struct adv748x_reg_value adv748x_sw_reset[] = {
  287. {ADV748X_PAGE_IO, 0xff, 0xff}, /* SW reset */
  288. {ADV748X_PAGE_WAIT, 0x00, 0x05},/* delay 5 */
  289. {ADV748X_PAGE_IO, 0x01, 0x76}, /* ADI Required Write */
  290. {ADV748X_PAGE_IO, 0xf2, 0x01}, /* Enable I2C Read Auto-Increment */
  291. {ADV748X_PAGE_EOR, 0xff, 0xff} /* End of register table */
  292. };
  293. /* Supported Formats For Script Below */
  294. /* - 01-29 HDMI to MIPI TxA CSI 4-Lane - RGB888: */
  295. static const struct adv748x_reg_value adv748x_init_txa_4lane[] = {
  296. /* Disable chip powerdown & Enable HDMI Rx block */
  297. {ADV748X_PAGE_IO, 0x00, 0x40},
  298. {ADV748X_PAGE_REPEATER, 0x40, 0x83}, /* Enable HDCP 1.1 */
  299. {ADV748X_PAGE_HDMI, 0x00, 0x08},/* Foreground Channel = A */
  300. {ADV748X_PAGE_HDMI, 0x98, 0xff},/* ADI Required Write */
  301. {ADV748X_PAGE_HDMI, 0x99, 0xa3},/* ADI Required Write */
  302. {ADV748X_PAGE_HDMI, 0x9a, 0x00},/* ADI Required Write */
  303. {ADV748X_PAGE_HDMI, 0x9b, 0x0a},/* ADI Required Write */
  304. {ADV748X_PAGE_HDMI, 0x9d, 0x40},/* ADI Required Write */
  305. {ADV748X_PAGE_HDMI, 0xcb, 0x09},/* ADI Required Write */
  306. {ADV748X_PAGE_HDMI, 0x3d, 0x10},/* ADI Required Write */
  307. {ADV748X_PAGE_HDMI, 0x3e, 0x7b},/* ADI Required Write */
  308. {ADV748X_PAGE_HDMI, 0x3f, 0x5e},/* ADI Required Write */
  309. {ADV748X_PAGE_HDMI, 0x4e, 0xfe},/* ADI Required Write */
  310. {ADV748X_PAGE_HDMI, 0x4f, 0x18},/* ADI Required Write */
  311. {ADV748X_PAGE_HDMI, 0x57, 0xa3},/* ADI Required Write */
  312. {ADV748X_PAGE_HDMI, 0x58, 0x04},/* ADI Required Write */
  313. {ADV748X_PAGE_HDMI, 0x85, 0x10},/* ADI Required Write */
  314. {ADV748X_PAGE_HDMI, 0x83, 0x00},/* Enable All Terminations */
  315. {ADV748X_PAGE_HDMI, 0xa3, 0x01},/* ADI Required Write */
  316. {ADV748X_PAGE_HDMI, 0xbe, 0x00},/* ADI Required Write */
  317. {ADV748X_PAGE_HDMI, 0x6c, 0x01},/* HPA Manual Enable */
  318. {ADV748X_PAGE_HDMI, 0xf8, 0x01},/* HPA Asserted */
  319. {ADV748X_PAGE_HDMI, 0x0f, 0x00},/* Audio Mute Speed Set to Fastest */
  320. /* (Smallest Step Size) */
  321. {ADV748X_PAGE_IO, 0x04, 0x02}, /* RGB Out of CP */
  322. {ADV748X_PAGE_IO, 0x12, 0xf0}, /* CSC Depends on ip Packets, SDR 444 */
  323. {ADV748X_PAGE_IO, 0x17, 0x80}, /* Luma & Chroma can reach 254d */
  324. {ADV748X_PAGE_IO, 0x03, 0x86}, /* CP-Insert_AV_Code */
  325. {ADV748X_PAGE_CP, 0x7c, 0x00}, /* ADI Required Write */
  326. {ADV748X_PAGE_IO, 0x0c, 0xe0}, /* Enable LLC_DLL & Double LLC Timing */
  327. {ADV748X_PAGE_IO, 0x0e, 0xdd}, /* LLC/PIX/SPI PINS TRISTATED AUD */
  328. /* Outputs Enabled */
  329. {ADV748X_PAGE_IO, 0x10, 0xa0}, /* Enable 4-lane CSI Tx & Pixel Port */
  330. {ADV748X_PAGE_TXA, 0x00, 0x84}, /* Enable 4-lane MIPI */
  331. {ADV748X_PAGE_TXA, 0x00, 0xa4}, /* Set Auto DPHY Timing */
  332. {ADV748X_PAGE_TXA, 0xdb, 0x10}, /* ADI Required Write */
  333. {ADV748X_PAGE_TXA, 0xd6, 0x07}, /* ADI Required Write */
  334. {ADV748X_PAGE_TXA, 0xc4, 0x0a}, /* ADI Required Write */
  335. {ADV748X_PAGE_TXA, 0x71, 0x33}, /* ADI Required Write */
  336. {ADV748X_PAGE_TXA, 0x72, 0x11}, /* ADI Required Write */
  337. {ADV748X_PAGE_TXA, 0xf0, 0x00}, /* i2c_dphy_pwdn - 1'b0 */
  338. {ADV748X_PAGE_TXA, 0x31, 0x82}, /* ADI Required Write */
  339. {ADV748X_PAGE_TXA, 0x1e, 0x40}, /* ADI Required Write */
  340. {ADV748X_PAGE_TXA, 0xda, 0x01}, /* i2c_mipi_pll_en - 1'b1 */
  341. {ADV748X_PAGE_WAIT, 0x00, 0x02},/* delay 2 */
  342. {ADV748X_PAGE_TXA, 0x00, 0x24 },/* Power-up CSI-TX */
  343. {ADV748X_PAGE_WAIT, 0x00, 0x01},/* delay 1 */
  344. {ADV748X_PAGE_TXA, 0xc1, 0x2b}, /* ADI Required Write */
  345. {ADV748X_PAGE_WAIT, 0x00, 0x01},/* delay 1 */
  346. {ADV748X_PAGE_TXA, 0x31, 0x80}, /* ADI Required Write */
  347. {ADV748X_PAGE_EOR, 0xff, 0xff} /* End of register table */
  348. };
  349. /* 02-01 Analog CVBS to MIPI TX-B CSI 1-Lane - */
  350. /* Autodetect CVBS Single Ended In Ain 1 - MIPI Out */
  351. static const struct adv748x_reg_value adv748x_init_txb_1lane[] = {
  352. {ADV748X_PAGE_IO, 0x00, 0x30}, /* Disable chip powerdown Rx */
  353. {ADV748X_PAGE_IO, 0xf2, 0x01}, /* Enable I2C Read Auto-Increment */
  354. {ADV748X_PAGE_IO, 0x0e, 0xff}, /* LLC/PIX/AUD/SPI PINS TRISTATED */
  355. {ADV748X_PAGE_SDP, 0x0f, 0x00}, /* Exit Power Down Mode */
  356. {ADV748X_PAGE_SDP, 0x52, 0xcd}, /* ADI Required Write */
  357. {ADV748X_PAGE_SDP, 0x0e, 0x80}, /* ADI Required Write */
  358. {ADV748X_PAGE_SDP, 0x9c, 0x00}, /* ADI Required Write */
  359. {ADV748X_PAGE_SDP, 0x9c, 0xff}, /* ADI Required Write */
  360. {ADV748X_PAGE_SDP, 0x0e, 0x00}, /* ADI Required Write */
  361. /* ADI recommended writes for improved video quality */
  362. {ADV748X_PAGE_SDP, 0x80, 0x51}, /* ADI Required Write */
  363. {ADV748X_PAGE_SDP, 0x81, 0x51}, /* ADI Required Write */
  364. {ADV748X_PAGE_SDP, 0x82, 0x68}, /* ADI Required Write */
  365. {ADV748X_PAGE_SDP, 0x03, 0x42}, /* Tri-S Output , PwrDwn 656 pads */
  366. {ADV748X_PAGE_SDP, 0x04, 0xb5}, /* ITU-R BT.656-4 compatible */
  367. {ADV748X_PAGE_SDP, 0x13, 0x00}, /* ADI Required Write */
  368. {ADV748X_PAGE_SDP, 0x17, 0x41}, /* Select SH1 */
  369. {ADV748X_PAGE_SDP, 0x31, 0x12}, /* ADI Required Write */
  370. {ADV748X_PAGE_SDP, 0xe6, 0x4f}, /* V bit end pos manually in NTSC */
  371. /* Enable 1-Lane MIPI Tx, */
  372. /* enable pixel output and route SD through Pixel port */
  373. {ADV748X_PAGE_IO, 0x10, 0x70},
  374. {ADV748X_PAGE_TXB, 0x00, 0x81}, /* Enable 1-lane MIPI */
  375. {ADV748X_PAGE_TXB, 0x00, 0xa1}, /* Set Auto DPHY Timing */
  376. {ADV748X_PAGE_TXB, 0xd2, 0x40}, /* ADI Required Write */
  377. {ADV748X_PAGE_TXB, 0xc4, 0x0a}, /* ADI Required Write */
  378. {ADV748X_PAGE_TXB, 0x71, 0x33}, /* ADI Required Write */
  379. {ADV748X_PAGE_TXB, 0x72, 0x11}, /* ADI Required Write */
  380. {ADV748X_PAGE_TXB, 0xf0, 0x00}, /* i2c_dphy_pwdn - 1'b0 */
  381. {ADV748X_PAGE_TXB, 0x31, 0x82}, /* ADI Required Write */
  382. {ADV748X_PAGE_TXB, 0x1e, 0x40}, /* ADI Required Write */
  383. {ADV748X_PAGE_TXB, 0xda, 0x01}, /* i2c_mipi_pll_en - 1'b1 */
  384. {ADV748X_PAGE_WAIT, 0x00, 0x02},/* delay 2 */
  385. {ADV748X_PAGE_TXB, 0x00, 0x21 },/* Power-up CSI-TX */
  386. {ADV748X_PAGE_WAIT, 0x00, 0x01},/* delay 1 */
  387. {ADV748X_PAGE_TXB, 0xc1, 0x2b}, /* ADI Required Write */
  388. {ADV748X_PAGE_WAIT, 0x00, 0x01},/* delay 1 */
  389. {ADV748X_PAGE_TXB, 0x31, 0x80}, /* ADI Required Write */
  390. {ADV748X_PAGE_EOR, 0xff, 0xff} /* End of register table */
  391. };
  392. static int adv748x_reset(struct adv748x_state *state)
  393. {
  394. int ret;
  395. ret = adv748x_write_regs(state, adv748x_sw_reset);
  396. if (ret < 0)
  397. return ret;
  398. ret = adv748x_set_slave_addresses(state);
  399. if (ret < 0)
  400. return ret;
  401. /* Init and power down TXA */
  402. ret = adv748x_write_regs(state, adv748x_init_txa_4lane);
  403. if (ret)
  404. return ret;
  405. adv748x_txa_power(state, 0);
  406. /* Init and power down TXB */
  407. ret = adv748x_write_regs(state, adv748x_init_txb_1lane);
  408. if (ret)
  409. return ret;
  410. adv748x_txb_power(state, 0);
  411. /* Disable chip powerdown & Enable HDMI Rx block */
  412. io_write(state, ADV748X_IO_PD, ADV748X_IO_PD_RX_EN);
  413. /* Enable 4-lane CSI Tx & Pixel Port */
  414. io_write(state, ADV748X_IO_10, ADV748X_IO_10_CSI4_EN |
  415. ADV748X_IO_10_CSI1_EN |
  416. ADV748X_IO_10_PIX_OUT_EN);
  417. /* Use vid_std and v_freq as freerun resolution for CP */
  418. cp_clrset(state, ADV748X_CP_CLMP_POS, ADV748X_CP_CLMP_POS_DIS_AUTO,
  419. ADV748X_CP_CLMP_POS_DIS_AUTO);
  420. return 0;
  421. }
  422. static int adv748x_identify_chip(struct adv748x_state *state)
  423. {
  424. int msb, lsb;
  425. lsb = io_read(state, ADV748X_IO_CHIP_REV_ID_1);
  426. msb = io_read(state, ADV748X_IO_CHIP_REV_ID_2);
  427. if (lsb < 0 || msb < 0) {
  428. adv_err(state, "Failed to read chip revision\n");
  429. return -EIO;
  430. }
  431. adv_info(state, "chip found @ 0x%02x revision %02x%02x\n",
  432. state->client->addr << 1, lsb, msb);
  433. return 0;
  434. }
  435. /* -----------------------------------------------------------------------------
  436. * i2c driver
  437. */
  438. void adv748x_subdev_init(struct v4l2_subdev *sd, struct adv748x_state *state,
  439. const struct v4l2_subdev_ops *ops, u32 function,
  440. const char *ident)
  441. {
  442. v4l2_subdev_init(sd, ops);
  443. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  444. /* the owner is the same as the i2c_client's driver owner */
  445. sd->owner = state->dev->driver->owner;
  446. sd->dev = state->dev;
  447. v4l2_set_subdevdata(sd, state);
  448. /* initialize name */
  449. snprintf(sd->name, sizeof(sd->name), "%s %d-%04x %s",
  450. state->dev->driver->name,
  451. i2c_adapter_id(state->client->adapter),
  452. state->client->addr, ident);
  453. sd->entity.function = function;
  454. sd->entity.ops = &adv748x_media_ops;
  455. }
  456. static int adv748x_parse_dt(struct adv748x_state *state)
  457. {
  458. struct device_node *ep_np = NULL;
  459. struct of_endpoint ep;
  460. bool out_found = false;
  461. bool in_found = false;
  462. for_each_endpoint_of_node(state->dev->of_node, ep_np) {
  463. of_graph_parse_endpoint(ep_np, &ep);
  464. adv_info(state, "Endpoint %pOF on port %d", ep.local_node,
  465. ep.port);
  466. if (ep.port >= ADV748X_PORT_MAX) {
  467. adv_err(state, "Invalid endpoint %pOF on port %d",
  468. ep.local_node, ep.port);
  469. continue;
  470. }
  471. if (state->endpoints[ep.port]) {
  472. adv_err(state,
  473. "Multiple port endpoints are not supported");
  474. continue;
  475. }
  476. of_node_get(ep_np);
  477. state->endpoints[ep.port] = ep_np;
  478. /*
  479. * At least one input endpoint and one output endpoint shall
  480. * be defined.
  481. */
  482. if (ep.port < ADV748X_PORT_TXA)
  483. in_found = true;
  484. else
  485. out_found = true;
  486. }
  487. return in_found && out_found ? 0 : -ENODEV;
  488. }
  489. static void adv748x_dt_cleanup(struct adv748x_state *state)
  490. {
  491. unsigned int i;
  492. for (i = 0; i < ADV748X_PORT_MAX; i++)
  493. of_node_put(state->endpoints[i]);
  494. }
  495. static int adv748x_probe(struct i2c_client *client,
  496. const struct i2c_device_id *id)
  497. {
  498. struct adv748x_state *state;
  499. int ret;
  500. /* Check if the adapter supports the needed features */
  501. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  502. return -EIO;
  503. state = kzalloc(sizeof(struct adv748x_state), GFP_KERNEL);
  504. if (!state)
  505. return -ENOMEM;
  506. mutex_init(&state->mutex);
  507. state->dev = &client->dev;
  508. state->client = client;
  509. state->i2c_clients[ADV748X_PAGE_IO] = client;
  510. i2c_set_clientdata(client, state);
  511. /*
  512. * We can not use container_of to get back to the state with two TXs;
  513. * Initialize the TXs's fields unconditionally on the endpoint
  514. * presence to access them later.
  515. */
  516. state->txa.state = state->txb.state = state;
  517. state->txa.page = ADV748X_PAGE_TXA;
  518. state->txb.page = ADV748X_PAGE_TXB;
  519. state->txa.port = ADV748X_PORT_TXA;
  520. state->txb.port = ADV748X_PORT_TXB;
  521. /* Discover and process ports declared by the Device tree endpoints */
  522. ret = adv748x_parse_dt(state);
  523. if (ret) {
  524. adv_err(state, "Failed to parse device tree");
  525. goto err_free_mutex;
  526. }
  527. /* Configure IO Regmap region */
  528. ret = adv748x_configure_regmap(state, ADV748X_PAGE_IO);
  529. if (ret) {
  530. adv_err(state, "Error configuring IO regmap region");
  531. goto err_cleanup_dt;
  532. }
  533. ret = adv748x_identify_chip(state);
  534. if (ret) {
  535. adv_err(state, "Failed to identify chip");
  536. goto err_cleanup_dt;
  537. }
  538. /* Configure remaining pages as I2C clients with regmap access */
  539. ret = adv748x_initialise_clients(state);
  540. if (ret) {
  541. adv_err(state, "Failed to setup client regmap pages");
  542. goto err_cleanup_clients;
  543. }
  544. /* SW reset ADV748X to its default values */
  545. ret = adv748x_reset(state);
  546. if (ret) {
  547. adv_err(state, "Failed to reset hardware");
  548. goto err_cleanup_clients;
  549. }
  550. /* Initialise HDMI */
  551. ret = adv748x_hdmi_init(&state->hdmi);
  552. if (ret) {
  553. adv_err(state, "Failed to probe HDMI");
  554. goto err_cleanup_clients;
  555. }
  556. /* Initialise AFE */
  557. ret = adv748x_afe_init(&state->afe);
  558. if (ret) {
  559. adv_err(state, "Failed to probe AFE");
  560. goto err_cleanup_hdmi;
  561. }
  562. /* Initialise TXA */
  563. ret = adv748x_csi2_init(state, &state->txa);
  564. if (ret) {
  565. adv_err(state, "Failed to probe TXA");
  566. goto err_cleanup_afe;
  567. }
  568. /* Initialise TXB */
  569. ret = adv748x_csi2_init(state, &state->txb);
  570. if (ret) {
  571. adv_err(state, "Failed to probe TXB");
  572. goto err_cleanup_txa;
  573. }
  574. return 0;
  575. err_cleanup_txa:
  576. adv748x_csi2_cleanup(&state->txa);
  577. err_cleanup_afe:
  578. adv748x_afe_cleanup(&state->afe);
  579. err_cleanup_hdmi:
  580. adv748x_hdmi_cleanup(&state->hdmi);
  581. err_cleanup_clients:
  582. adv748x_unregister_clients(state);
  583. err_cleanup_dt:
  584. adv748x_dt_cleanup(state);
  585. err_free_mutex:
  586. mutex_destroy(&state->mutex);
  587. kfree(state);
  588. return ret;
  589. }
  590. static int adv748x_remove(struct i2c_client *client)
  591. {
  592. struct adv748x_state *state = i2c_get_clientdata(client);
  593. adv748x_afe_cleanup(&state->afe);
  594. adv748x_hdmi_cleanup(&state->hdmi);
  595. adv748x_csi2_cleanup(&state->txa);
  596. adv748x_csi2_cleanup(&state->txb);
  597. adv748x_unregister_clients(state);
  598. adv748x_dt_cleanup(state);
  599. mutex_destroy(&state->mutex);
  600. kfree(state);
  601. return 0;
  602. }
  603. static const struct i2c_device_id adv748x_id[] = {
  604. { "adv7481", 0 },
  605. { "adv7482", 0 },
  606. { },
  607. };
  608. MODULE_DEVICE_TABLE(i2c, adv748x_id);
  609. static const struct of_device_id adv748x_of_table[] = {
  610. { .compatible = "adi,adv7481", },
  611. { .compatible = "adi,adv7482", },
  612. { }
  613. };
  614. MODULE_DEVICE_TABLE(of, adv748x_of_table);
  615. static struct i2c_driver adv748x_driver = {
  616. .driver = {
  617. .name = "adv748x",
  618. .of_match_table = adv748x_of_table,
  619. },
  620. .probe = adv748x_probe,
  621. .remove = adv748x_remove,
  622. .id_table = adv748x_id,
  623. };
  624. module_i2c_driver(adv748x_driver);
  625. MODULE_AUTHOR("Kieran Bingham <kieran.bingham@ideasonboard.com>");
  626. MODULE_DESCRIPTION("ADV748X video decoder");
  627. MODULE_LICENSE("GPL v2");