adv748x.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Driver for Analog Devices ADV748X video decoder and HDMI receiver
  4. *
  5. * Copyright (C) 2017 Renesas Electronics Corp.
  6. *
  7. * Authors:
  8. * Koji Matsuoka <[email protected]>
  9. * Niklas Söderlund <[email protected]>
  10. * Kieran Bingham <[email protected]>
  11. *
  12. * The ADV748x range of receivers have the following configurations:
  13. *
  14. * Analog HDMI MHL 4-Lane 1-Lane
  15. * In In CSI CSI
  16. * ADV7480 X X X
  17. * ADV7481 X X X X X
  18. * ADV7482 X X X X
  19. */
  20. #include <linux/i2c.h>
  21. #ifndef _ADV748X_H_
  22. #define _ADV748X_H_
  23. enum adv748x_page {
  24. ADV748X_PAGE_IO,
  25. ADV748X_PAGE_DPLL,
  26. ADV748X_PAGE_CP,
  27. ADV748X_PAGE_HDMI,
  28. ADV748X_PAGE_EDID,
  29. ADV748X_PAGE_REPEATER,
  30. ADV748X_PAGE_INFOFRAME,
  31. ADV748X_PAGE_CBUS,
  32. ADV748X_PAGE_CEC,
  33. ADV748X_PAGE_SDP,
  34. ADV748X_PAGE_TXB,
  35. ADV748X_PAGE_TXA,
  36. ADV748X_PAGE_MAX,
  37. /* Fake pages for register sequences */
  38. ADV748X_PAGE_EOR, /* End Mark */
  39. };
  40. /*
  41. * Device tree port number definitions
  42. *
  43. * The ADV748X ports define the mapping between subdevices
  44. * and the device tree specification
  45. */
  46. enum adv748x_ports {
  47. ADV748X_PORT_AIN0 = 0,
  48. ADV748X_PORT_AIN1 = 1,
  49. ADV748X_PORT_AIN2 = 2,
  50. ADV748X_PORT_AIN3 = 3,
  51. ADV748X_PORT_AIN4 = 4,
  52. ADV748X_PORT_AIN5 = 5,
  53. ADV748X_PORT_AIN6 = 6,
  54. ADV748X_PORT_AIN7 = 7,
  55. ADV748X_PORT_HDMI = 8,
  56. ADV748X_PORT_TTL = 9,
  57. ADV748X_PORT_TXA = 10,
  58. ADV748X_PORT_TXB = 11,
  59. ADV748X_PORT_MAX = 12,
  60. };
  61. enum adv748x_csi2_pads {
  62. ADV748X_CSI2_SINK,
  63. ADV748X_CSI2_SOURCE,
  64. ADV748X_CSI2_NR_PADS,
  65. };
  66. /* CSI2 transmitters can have 2 internal connections, HDMI/AFE */
  67. #define ADV748X_CSI2_MAX_SUBDEVS 2
  68. struct adv748x_csi2 {
  69. struct adv748x_state *state;
  70. struct v4l2_mbus_framefmt format;
  71. unsigned int page;
  72. unsigned int port;
  73. unsigned int num_lanes;
  74. unsigned int active_lanes;
  75. struct media_pad pads[ADV748X_CSI2_NR_PADS];
  76. struct v4l2_ctrl_handler ctrl_hdl;
  77. struct v4l2_ctrl *pixel_rate;
  78. struct v4l2_subdev *src;
  79. struct v4l2_subdev sd;
  80. };
  81. #define notifier_to_csi2(n) container_of(n, struct adv748x_csi2, notifier)
  82. #define adv748x_sd_to_csi2(sd) container_of(sd, struct adv748x_csi2, sd)
  83. #define is_tx_enabled(_tx) ((_tx)->state->endpoints[(_tx)->port] != NULL)
  84. #define is_txa(_tx) ((_tx) == &(_tx)->state->txa)
  85. #define is_txb(_tx) ((_tx) == &(_tx)->state->txb)
  86. #define is_tx(_tx) (is_txa(_tx) || is_txb(_tx))
  87. #define is_afe_enabled(_state) \
  88. ((_state)->endpoints[ADV748X_PORT_AIN0] != NULL || \
  89. (_state)->endpoints[ADV748X_PORT_AIN1] != NULL || \
  90. (_state)->endpoints[ADV748X_PORT_AIN2] != NULL || \
  91. (_state)->endpoints[ADV748X_PORT_AIN3] != NULL || \
  92. (_state)->endpoints[ADV748X_PORT_AIN4] != NULL || \
  93. (_state)->endpoints[ADV748X_PORT_AIN5] != NULL || \
  94. (_state)->endpoints[ADV748X_PORT_AIN6] != NULL || \
  95. (_state)->endpoints[ADV748X_PORT_AIN7] != NULL)
  96. #define is_hdmi_enabled(_state) ((_state)->endpoints[ADV748X_PORT_HDMI] != NULL)
  97. enum adv748x_hdmi_pads {
  98. ADV748X_HDMI_SINK,
  99. ADV748X_HDMI_SOURCE,
  100. ADV748X_HDMI_NR_PADS,
  101. };
  102. struct adv748x_hdmi {
  103. struct media_pad pads[ADV748X_HDMI_NR_PADS];
  104. struct v4l2_ctrl_handler ctrl_hdl;
  105. struct v4l2_subdev sd;
  106. struct v4l2_mbus_framefmt format;
  107. struct v4l2_dv_timings timings;
  108. struct v4l2_fract aspect_ratio;
  109. struct adv748x_csi2 *tx;
  110. struct {
  111. u8 edid[512];
  112. u32 present;
  113. unsigned int blocks;
  114. } edid;
  115. };
  116. #define adv748x_ctrl_to_hdmi(ctrl) \
  117. container_of(ctrl->handler, struct adv748x_hdmi, ctrl_hdl)
  118. #define adv748x_sd_to_hdmi(sd) container_of(sd, struct adv748x_hdmi, sd)
  119. enum adv748x_afe_pads {
  120. ADV748X_AFE_SINK_AIN0,
  121. ADV748X_AFE_SINK_AIN1,
  122. ADV748X_AFE_SINK_AIN2,
  123. ADV748X_AFE_SINK_AIN3,
  124. ADV748X_AFE_SINK_AIN4,
  125. ADV748X_AFE_SINK_AIN5,
  126. ADV748X_AFE_SINK_AIN6,
  127. ADV748X_AFE_SINK_AIN7,
  128. ADV748X_AFE_SOURCE,
  129. ADV748X_AFE_NR_PADS,
  130. };
  131. struct adv748x_afe {
  132. struct media_pad pads[ADV748X_AFE_NR_PADS];
  133. struct v4l2_ctrl_handler ctrl_hdl;
  134. struct v4l2_subdev sd;
  135. struct v4l2_mbus_framefmt format;
  136. struct adv748x_csi2 *tx;
  137. bool streaming;
  138. v4l2_std_id curr_norm;
  139. unsigned int input;
  140. };
  141. #define adv748x_ctrl_to_afe(ctrl) \
  142. container_of(ctrl->handler, struct adv748x_afe, ctrl_hdl)
  143. #define adv748x_sd_to_afe(sd) container_of(sd, struct adv748x_afe, sd)
  144. /**
  145. * struct adv748x_state - State of ADV748X
  146. * @dev: (OF) device
  147. * @client: I2C client
  148. * @mutex: protect global state
  149. *
  150. * @endpoints: parsed device node endpoints for each port
  151. *
  152. * @i2c_addresses: I2C Page addresses
  153. * @i2c_clients: I2C clients for the page accesses
  154. * @regmap: regmap configuration pages.
  155. *
  156. * @hdmi: state of HDMI receiver context
  157. * @afe: state of AFE receiver context
  158. * @txa: state of TXA transmitter context
  159. * @txb: state of TXB transmitter context
  160. */
  161. struct adv748x_state {
  162. struct device *dev;
  163. struct i2c_client *client;
  164. struct mutex mutex;
  165. struct device_node *endpoints[ADV748X_PORT_MAX];
  166. struct i2c_client *i2c_clients[ADV748X_PAGE_MAX];
  167. struct regmap *regmap[ADV748X_PAGE_MAX];
  168. struct adv748x_hdmi hdmi;
  169. struct adv748x_afe afe;
  170. struct adv748x_csi2 txa;
  171. struct adv748x_csi2 txb;
  172. };
  173. #define adv748x_hdmi_to_state(h) container_of(h, struct adv748x_state, hdmi)
  174. #define adv748x_afe_to_state(a) container_of(a, struct adv748x_state, afe)
  175. #define adv_err(a, fmt, arg...) dev_err(a->dev, fmt, ##arg)
  176. #define adv_info(a, fmt, arg...) dev_info(a->dev, fmt, ##arg)
  177. #define adv_dbg(a, fmt, arg...) dev_dbg(a->dev, fmt, ##arg)
  178. /* Register Mappings */
  179. /* IO Map */
  180. #define ADV748X_IO_PD 0x00 /* power down controls */
  181. #define ADV748X_IO_PD_RX_EN BIT(6)
  182. #define ADV748X_IO_REG_01 0x01 /* pwrdn{2}b, prog_xtal_freq */
  183. #define ADV748X_IO_REG_01_PWRDN_MASK (BIT(7) | BIT(6))
  184. #define ADV748X_IO_REG_01_PWRDN2B BIT(7) /* CEC Wakeup Support */
  185. #define ADV748X_IO_REG_01_PWRDNB BIT(6) /* CEC Wakeup Support */
  186. #define ADV748X_IO_REG_04 0x04
  187. #define ADV748X_IO_REG_04_FORCE_FR BIT(0) /* Force CP free-run */
  188. #define ADV748X_IO_DATAPATH 0x03 /* datapath cntrl */
  189. #define ADV748X_IO_DATAPATH_VFREQ_M 0x70
  190. #define ADV748X_IO_DATAPATH_VFREQ_SHIFT 4
  191. #define ADV748X_IO_VID_STD 0x05
  192. #define ADV748X_IO_10 0x10 /* io_reg_10 */
  193. #define ADV748X_IO_10_CSI4_EN BIT(7)
  194. #define ADV748X_IO_10_CSI1_EN BIT(6)
  195. #define ADV748X_IO_10_PIX_OUT_EN BIT(5)
  196. #define ADV748X_IO_10_CSI4_IN_SEL_AFE BIT(3)
  197. #define ADV748X_IO_CHIP_REV_ID_1 0xdf
  198. #define ADV748X_IO_CHIP_REV_ID_2 0xe0
  199. #define ADV748X_IO_REG_F2 0xf2
  200. #define ADV748X_IO_REG_F2_READ_AUTO_INC BIT(0)
  201. /* For PAGE slave address offsets */
  202. #define ADV748X_IO_SLAVE_ADDR_BASE 0xf2
  203. /*
  204. * The ADV748x_Recommended_Settings_PrA_2014-08-20.pdf details both 0x80 and
  205. * 0xff as examples for performing a software reset.
  206. */
  207. #define ADV748X_IO_REG_FF 0xff
  208. #define ADV748X_IO_REG_FF_MAIN_RESET 0xff
  209. /* HDMI RX Map */
  210. #define ADV748X_HDMI_LW1 0x07 /* line width_1 */
  211. #define ADV748X_HDMI_LW1_VERT_FILTER BIT(7)
  212. #define ADV748X_HDMI_LW1_DE_REGEN BIT(5)
  213. #define ADV748X_HDMI_LW1_WIDTH_MASK 0x1fff
  214. #define ADV748X_HDMI_F0H1 0x09 /* field0 height_1 */
  215. #define ADV748X_HDMI_F0H1_HEIGHT_MASK 0x1fff
  216. #define ADV748X_HDMI_F1H1 0x0b /* field1 height_1 */
  217. #define ADV748X_HDMI_F1H1_INTERLACED BIT(5)
  218. #define ADV748X_HDMI_HFRONT_PORCH 0x20 /* hsync_front_porch_1 */
  219. #define ADV748X_HDMI_HFRONT_PORCH_MASK 0x1fff
  220. #define ADV748X_HDMI_HSYNC_WIDTH 0x22 /* hsync_pulse_width_1 */
  221. #define ADV748X_HDMI_HSYNC_WIDTH_MASK 0x1fff
  222. #define ADV748X_HDMI_HBACK_PORCH 0x24 /* hsync_back_porch_1 */
  223. #define ADV748X_HDMI_HBACK_PORCH_MASK 0x1fff
  224. #define ADV748X_HDMI_VFRONT_PORCH 0x2a /* field0_vs_front_porch_1 */
  225. #define ADV748X_HDMI_VFRONT_PORCH_MASK 0x3fff
  226. #define ADV748X_HDMI_VSYNC_WIDTH 0x2e /* field0_vs_pulse_width_1 */
  227. #define ADV748X_HDMI_VSYNC_WIDTH_MASK 0x3fff
  228. #define ADV748X_HDMI_VBACK_PORCH 0x32 /* field0_vs_back_porch_1 */
  229. #define ADV748X_HDMI_VBACK_PORCH_MASK 0x3fff
  230. #define ADV748X_HDMI_TMDS_1 0x51 /* hdmi_reg_51 */
  231. #define ADV748X_HDMI_TMDS_2 0x52 /* hdmi_reg_52 */
  232. /* HDMI RX Repeater Map */
  233. #define ADV748X_REPEATER_EDID_SZ 0x70 /* primary_edid_size */
  234. #define ADV748X_REPEATER_EDID_SZ_SHIFT 4
  235. #define ADV748X_REPEATER_EDID_CTL 0x74 /* hdcp edid controls */
  236. #define ADV748X_REPEATER_EDID_CTL_EN BIT(0) /* man_edid_a_enable */
  237. /* SDP Main Map */
  238. #define ADV748X_SDP_INSEL 0x00 /* user_map_rw_reg_00 */
  239. #define ADV748X_SDP_VID_SEL 0x02 /* user_map_rw_reg_02 */
  240. #define ADV748X_SDP_VID_SEL_MASK 0xf0
  241. #define ADV748X_SDP_VID_SEL_SHIFT 4
  242. /* Contrast - Unsigned*/
  243. #define ADV748X_SDP_CON 0x08 /* user_map_rw_reg_08 */
  244. #define ADV748X_SDP_CON_MIN 0
  245. #define ADV748X_SDP_CON_DEF 128
  246. #define ADV748X_SDP_CON_MAX 255
  247. /* Brightness - Signed */
  248. #define ADV748X_SDP_BRI 0x0a /* user_map_rw_reg_0a */
  249. #define ADV748X_SDP_BRI_MIN -128
  250. #define ADV748X_SDP_BRI_DEF 0
  251. #define ADV748X_SDP_BRI_MAX 127
  252. /* Hue - Signed, inverted*/
  253. #define ADV748X_SDP_HUE 0x0b /* user_map_rw_reg_0b */
  254. #define ADV748X_SDP_HUE_MIN -127
  255. #define ADV748X_SDP_HUE_DEF 0
  256. #define ADV748X_SDP_HUE_MAX 128
  257. /* Test Patterns / Default Values */
  258. #define ADV748X_SDP_DEF 0x0c /* user_map_rw_reg_0c */
  259. #define ADV748X_SDP_DEF_VAL_EN BIT(0) /* Force free run mode */
  260. #define ADV748X_SDP_DEF_VAL_AUTO_EN BIT(1) /* Free run when no signal */
  261. #define ADV748X_SDP_MAP_SEL 0x0e /* user_map_rw_reg_0e */
  262. #define ADV748X_SDP_MAP_SEL_RO_MAIN 1
  263. /* Free run pattern select */
  264. #define ADV748X_SDP_FRP 0x14
  265. #define ADV748X_SDP_FRP_MASK GENMASK(3, 1)
  266. /* Saturation */
  267. #define ADV748X_SDP_SD_SAT_U 0xe3 /* user_map_rw_reg_e3 */
  268. #define ADV748X_SDP_SD_SAT_V 0xe4 /* user_map_rw_reg_e4 */
  269. #define ADV748X_SDP_SAT_MIN 0
  270. #define ADV748X_SDP_SAT_DEF 128
  271. #define ADV748X_SDP_SAT_MAX 255
  272. /* SDP RO Main Map */
  273. #define ADV748X_SDP_RO_10 0x10
  274. #define ADV748X_SDP_RO_10_IN_LOCK BIT(0)
  275. /* CP Map */
  276. #define ADV748X_CP_PAT_GEN 0x37 /* int_pat_gen_1 */
  277. #define ADV748X_CP_PAT_GEN_EN BIT(7)
  278. /* Contrast Control - Unsigned */
  279. #define ADV748X_CP_CON 0x3a /* contrast_cntrl */
  280. #define ADV748X_CP_CON_MIN 0 /* Minimum contrast */
  281. #define ADV748X_CP_CON_DEF 128 /* Default */
  282. #define ADV748X_CP_CON_MAX 255 /* Maximum contrast */
  283. /* Saturation Control - Unsigned */
  284. #define ADV748X_CP_SAT 0x3b /* saturation_cntrl */
  285. #define ADV748X_CP_SAT_MIN 0 /* Minimum saturation */
  286. #define ADV748X_CP_SAT_DEF 128 /* Default */
  287. #define ADV748X_CP_SAT_MAX 255 /* Maximum saturation */
  288. /* Brightness Control - Signed */
  289. #define ADV748X_CP_BRI 0x3c /* brightness_cntrl */
  290. #define ADV748X_CP_BRI_MIN -128 /* Luma is -512d */
  291. #define ADV748X_CP_BRI_DEF 0 /* Luma is 0 */
  292. #define ADV748X_CP_BRI_MAX 127 /* Luma is 508d */
  293. /* Hue Control */
  294. #define ADV748X_CP_HUE 0x3d /* hue_cntrl */
  295. #define ADV748X_CP_HUE_MIN 0 /* -90 degree */
  296. #define ADV748X_CP_HUE_DEF 0 /* -90 degree */
  297. #define ADV748X_CP_HUE_MAX 255 /* +90 degree */
  298. #define ADV748X_CP_VID_ADJ 0x3e /* vid_adj_0 */
  299. #define ADV748X_CP_VID_ADJ_ENABLE BIT(7) /* Enable colour controls */
  300. #define ADV748X_CP_DE_POS_HIGH 0x8b /* de_pos_adj_6 */
  301. #define ADV748X_CP_DE_POS_HIGH_SET BIT(6)
  302. #define ADV748X_CP_DE_POS_END_LOW 0x8c /* de_pos_adj_7 */
  303. #define ADV748X_CP_DE_POS_START_LOW 0x8d /* de_pos_adj_8 */
  304. #define ADV748X_CP_VID_ADJ_2 0x91
  305. #define ADV748X_CP_VID_ADJ_2_INTERLACED BIT(6)
  306. #define ADV748X_CP_VID_ADJ_2_INTERLACED_3D BIT(4)
  307. #define ADV748X_CP_CLMP_POS 0xc9 /* clmp_pos_cntrl_4 */
  308. #define ADV748X_CP_CLMP_POS_DIS_AUTO BIT(0) /* dis_auto_param_buff */
  309. /* CSI : TXA/TXB Maps */
  310. #define ADV748X_CSI_VC_REF 0x0d /* csi_tx_top_reg_0d */
  311. #define ADV748X_CSI_VC_REF_SHIFT 6
  312. #define ADV748X_CSI_FS_AS_LS 0x1e /* csi_tx_top_reg_1e */
  313. #define ADV748X_CSI_FS_AS_LS_UNKNOWN BIT(6) /* Undocumented bit */
  314. /* Register handling */
  315. int adv748x_read(struct adv748x_state *state, u8 addr, u8 reg);
  316. int adv748x_write(struct adv748x_state *state, u8 page, u8 reg, u8 value);
  317. int adv748x_write_block(struct adv748x_state *state, int client_page,
  318. unsigned int init_reg, const void *val,
  319. size_t val_len);
  320. #define io_read(s, r) adv748x_read(s, ADV748X_PAGE_IO, r)
  321. #define io_write(s, r, v) adv748x_write(s, ADV748X_PAGE_IO, r, v)
  322. #define io_clrset(s, r, m, v) io_write(s, r, (io_read(s, r) & ~(m)) | (v))
  323. #define hdmi_read(s, r) adv748x_read(s, ADV748X_PAGE_HDMI, r)
  324. #define hdmi_read16(s, r, m) (((hdmi_read(s, r) << 8) | hdmi_read(s, (r)+1)) & (m))
  325. #define hdmi_write(s, r, v) adv748x_write(s, ADV748X_PAGE_HDMI, r, v)
  326. #define repeater_read(s, r) adv748x_read(s, ADV748X_PAGE_REPEATER, r)
  327. #define repeater_write(s, r, v) adv748x_write(s, ADV748X_PAGE_REPEATER, r, v)
  328. #define sdp_read(s, r) adv748x_read(s, ADV748X_PAGE_SDP, r)
  329. #define sdp_write(s, r, v) adv748x_write(s, ADV748X_PAGE_SDP, r, v)
  330. #define sdp_clrset(s, r, m, v) sdp_write(s, r, (sdp_read(s, r) & ~(m)) | (v))
  331. #define cp_read(s, r) adv748x_read(s, ADV748X_PAGE_CP, r)
  332. #define cp_write(s, r, v) adv748x_write(s, ADV748X_PAGE_CP, r, v)
  333. #define cp_clrset(s, r, m, v) cp_write(s, r, (cp_read(s, r) & ~(m)) | (v))
  334. #define tx_read(t, r) adv748x_read(t->state, t->page, r)
  335. #define tx_write(t, r, v) adv748x_write(t->state, t->page, r, v)
  336. static inline struct v4l2_subdev *adv748x_get_remote_sd(struct media_pad *pad)
  337. {
  338. pad = media_pad_remote_pad_first(pad);
  339. if (!pad)
  340. return NULL;
  341. return media_entity_to_v4l2_subdev(pad->entity);
  342. }
  343. void adv748x_subdev_init(struct v4l2_subdev *sd, struct adv748x_state *state,
  344. const struct v4l2_subdev_ops *ops, u32 function,
  345. const char *ident);
  346. int adv748x_register_subdevs(struct adv748x_state *state,
  347. struct v4l2_device *v4l2_dev);
  348. int adv748x_tx_power(struct adv748x_csi2 *tx, bool on);
  349. int adv748x_afe_init(struct adv748x_afe *afe);
  350. void adv748x_afe_cleanup(struct adv748x_afe *afe);
  351. int adv748x_afe_s_input(struct adv748x_afe *afe, unsigned int input);
  352. int adv748x_csi2_init(struct adv748x_state *state, struct adv748x_csi2 *tx);
  353. void adv748x_csi2_cleanup(struct adv748x_csi2 *tx);
  354. int adv748x_csi2_set_virtual_channel(struct adv748x_csi2 *tx, unsigned int vc);
  355. int adv748x_csi2_set_pixelrate(struct v4l2_subdev *sd, s64 rate);
  356. int adv748x_hdmi_init(struct adv748x_hdmi *hdmi);
  357. void adv748x_hdmi_cleanup(struct adv748x_hdmi *hdmi);
  358. #endif /* _ADV748X_H_ */