adv7180.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. /*
  2. * adv7180.c Analog Devices ADV7180 video decoder driver
  3. * Copyright (c) 2009 Intel Corporation
  4. * Copyright (C) 2013 Cogent Embedded, Inc.
  5. * Copyright (C) 2013 Renesas Solutions Corp.
  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 version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/errno.h>
  23. #include <linux/kernel.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/i2c.h>
  26. #include <linux/slab.h>
  27. #include <linux/of.h>
  28. #include <linux/gpio/consumer.h>
  29. #include <linux/videodev2.h>
  30. #include <media/v4l2-ioctl.h>
  31. #include <media/v4l2-event.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-ctrls.h>
  34. #include <linux/mutex.h>
  35. #include <linux/delay.h>
  36. #define ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM 0x0
  37. #define ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM_PED 0x1
  38. #define ADV7180_STD_AD_PAL_N_NTSC_J_SECAM 0x2
  39. #define ADV7180_STD_AD_PAL_N_NTSC_M_SECAM 0x3
  40. #define ADV7180_STD_NTSC_J 0x4
  41. #define ADV7180_STD_NTSC_M 0x5
  42. #define ADV7180_STD_PAL60 0x6
  43. #define ADV7180_STD_NTSC_443 0x7
  44. #define ADV7180_STD_PAL_BG 0x8
  45. #define ADV7180_STD_PAL_N 0x9
  46. #define ADV7180_STD_PAL_M 0xa
  47. #define ADV7180_STD_PAL_M_PED 0xb
  48. #define ADV7180_STD_PAL_COMB_N 0xc
  49. #define ADV7180_STD_PAL_COMB_N_PED 0xd
  50. #define ADV7180_STD_PAL_SECAM 0xe
  51. #define ADV7180_STD_PAL_SECAM_PED 0xf
  52. #define ADV7180_REG_INPUT_CONTROL 0x0000
  53. #define ADV7180_INPUT_CONTROL_INSEL_MASK 0x0f
  54. #define ADV7182_REG_INPUT_VIDSEL 0x0002
  55. #define ADV7180_REG_OUTPUT_CONTROL 0x0003
  56. #define ADV7180_REG_EXTENDED_OUTPUT_CONTROL 0x0004
  57. #define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5
  58. #define ADV7180_REG_AUTODETECT_ENABLE 0x0007
  59. #define ADV7180_AUTODETECT_DEFAULT 0x7f
  60. /* Contrast */
  61. #define ADV7180_REG_CON 0x0008 /*Unsigned */
  62. #define ADV7180_CON_MIN 0
  63. #define ADV7180_CON_DEF 128
  64. #define ADV7180_CON_MAX 255
  65. /* Brightness*/
  66. #define ADV7180_REG_BRI 0x000a /*Signed */
  67. #define ADV7180_BRI_MIN -128
  68. #define ADV7180_BRI_DEF 0
  69. #define ADV7180_BRI_MAX 127
  70. /* Hue */
  71. #define ADV7180_REG_HUE 0x000b /*Signed, inverted */
  72. #define ADV7180_HUE_MIN -127
  73. #define ADV7180_HUE_DEF 0
  74. #define ADV7180_HUE_MAX 128
  75. #define ADV7180_REG_CTRL 0x000e
  76. #define ADV7180_CTRL_IRQ_SPACE 0x20
  77. #define ADV7180_REG_PWR_MAN 0x0f
  78. #define ADV7180_PWR_MAN_ON 0x04
  79. #define ADV7180_PWR_MAN_OFF 0x24
  80. #define ADV7180_PWR_MAN_RES 0x80
  81. #define ADV7180_REG_STATUS1 0x0010
  82. #define ADV7180_STATUS1_IN_LOCK 0x01
  83. #define ADV7180_STATUS1_AUTOD_MASK 0x70
  84. #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
  85. #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
  86. #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
  87. #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
  88. #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
  89. #define ADV7180_STATUS1_AUTOD_SECAM 0x50
  90. #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
  91. #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
  92. #define ADV7180_REG_IDENT 0x0011
  93. #define ADV7180_ID_7180 0x18
  94. #define ADV7180_REG_STATUS3 0x0013
  95. #define ADV7180_REG_ANALOG_CLAMP_CTL 0x0014
  96. #define ADV7180_REG_SHAP_FILTER_CTL_1 0x0017
  97. #define ADV7180_REG_CTRL_2 0x001d
  98. #define ADV7180_REG_VSYNC_FIELD_CTL_1 0x0031
  99. #define ADV7180_REG_MANUAL_WIN_CTL_1 0x003d
  100. #define ADV7180_REG_MANUAL_WIN_CTL_2 0x003e
  101. #define ADV7180_REG_MANUAL_WIN_CTL_3 0x003f
  102. #define ADV7180_REG_LOCK_CNT 0x0051
  103. #define ADV7180_REG_CVBS_TRIM 0x0052
  104. #define ADV7180_REG_CLAMP_ADJ 0x005a
  105. #define ADV7180_REG_RES_CIR 0x005f
  106. #define ADV7180_REG_DIFF_MODE 0x0060
  107. #define ADV7180_REG_ICONF1 0x2040
  108. #define ADV7180_ICONF1_ACTIVE_LOW 0x01
  109. #define ADV7180_ICONF1_PSYNC_ONLY 0x10
  110. #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
  111. /* Saturation */
  112. #define ADV7180_REG_SD_SAT_CB 0x00e3 /*Unsigned */
  113. #define ADV7180_REG_SD_SAT_CR 0x00e4 /*Unsigned */
  114. #define ADV7180_SAT_MIN 0
  115. #define ADV7180_SAT_DEF 128
  116. #define ADV7180_SAT_MAX 255
  117. #define ADV7180_IRQ1_LOCK 0x01
  118. #define ADV7180_IRQ1_UNLOCK 0x02
  119. #define ADV7180_REG_ISR1 0x2042
  120. #define ADV7180_REG_ICR1 0x2043
  121. #define ADV7180_REG_IMR1 0x2044
  122. #define ADV7180_REG_IMR2 0x2048
  123. #define ADV7180_IRQ3_AD_CHANGE 0x08
  124. #define ADV7180_REG_ISR3 0x204A
  125. #define ADV7180_REG_ICR3 0x204B
  126. #define ADV7180_REG_IMR3 0x204C
  127. #define ADV7180_REG_IMR4 0x2050
  128. #define ADV7180_REG_NTSC_V_BIT_END 0x00E6
  129. #define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F
  130. #define ADV7180_REG_VPP_SLAVE_ADDR 0xFD
  131. #define ADV7180_REG_CSI_SLAVE_ADDR 0xFE
  132. #define ADV7180_REG_ACE_CTRL1 0x4080
  133. #define ADV7180_REG_ACE_CTRL5 0x4084
  134. #define ADV7180_REG_FLCONTROL 0x40e0
  135. #define ADV7180_FLCONTROL_FL_ENABLE 0x1
  136. #define ADV7180_REG_RST_CLAMP 0x809c
  137. #define ADV7180_REG_AGC_ADJ1 0x80b6
  138. #define ADV7180_REG_AGC_ADJ2 0x80c0
  139. #define ADV7180_CSI_REG_PWRDN 0x00
  140. #define ADV7180_CSI_PWRDN 0x80
  141. #define ADV7180_INPUT_CVBS_AIN1 0x00
  142. #define ADV7180_INPUT_CVBS_AIN2 0x01
  143. #define ADV7180_INPUT_CVBS_AIN3 0x02
  144. #define ADV7180_INPUT_CVBS_AIN4 0x03
  145. #define ADV7180_INPUT_CVBS_AIN5 0x04
  146. #define ADV7180_INPUT_CVBS_AIN6 0x05
  147. #define ADV7180_INPUT_SVIDEO_AIN1_AIN2 0x06
  148. #define ADV7180_INPUT_SVIDEO_AIN3_AIN4 0x07
  149. #define ADV7180_INPUT_SVIDEO_AIN5_AIN6 0x08
  150. #define ADV7180_INPUT_YPRPB_AIN1_AIN2_AIN3 0x09
  151. #define ADV7180_INPUT_YPRPB_AIN4_AIN5_AIN6 0x0a
  152. #define ADV7182_INPUT_CVBS_AIN1 0x00
  153. #define ADV7182_INPUT_CVBS_AIN2 0x01
  154. #define ADV7182_INPUT_CVBS_AIN3 0x02
  155. #define ADV7182_INPUT_CVBS_AIN4 0x03
  156. #define ADV7182_INPUT_CVBS_AIN5 0x04
  157. #define ADV7182_INPUT_CVBS_AIN6 0x05
  158. #define ADV7182_INPUT_CVBS_AIN7 0x06
  159. #define ADV7182_INPUT_CVBS_AIN8 0x07
  160. #define ADV7182_INPUT_SVIDEO_AIN1_AIN2 0x08
  161. #define ADV7182_INPUT_SVIDEO_AIN3_AIN4 0x09
  162. #define ADV7182_INPUT_SVIDEO_AIN5_AIN6 0x0a
  163. #define ADV7182_INPUT_SVIDEO_AIN7_AIN8 0x0b
  164. #define ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3 0x0c
  165. #define ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6 0x0d
  166. #define ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2 0x0e
  167. #define ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4 0x0f
  168. #define ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6 0x10
  169. #define ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8 0x11
  170. #define ADV7180_DEFAULT_CSI_I2C_ADDR 0x44
  171. #define ADV7180_DEFAULT_VPP_I2C_ADDR 0x42
  172. #define V4L2_CID_ADV_FAST_SWITCH (V4L2_CID_USER_ADV7180_BASE + 0x00)
  173. struct adv7180_state;
  174. #define ADV7180_FLAG_RESET_POWERED BIT(0)
  175. #define ADV7180_FLAG_V2 BIT(1)
  176. #define ADV7180_FLAG_MIPI_CSI2 BIT(2)
  177. #define ADV7180_FLAG_I2P BIT(3)
  178. struct adv7180_chip_info {
  179. unsigned int flags;
  180. unsigned int valid_input_mask;
  181. int (*set_std)(struct adv7180_state *st, unsigned int std);
  182. int (*select_input)(struct adv7180_state *st, unsigned int input);
  183. int (*init)(struct adv7180_state *state);
  184. };
  185. struct adv7180_state {
  186. struct v4l2_ctrl_handler ctrl_hdl;
  187. struct v4l2_subdev sd;
  188. struct media_pad pad;
  189. struct mutex mutex; /* mutual excl. when accessing chip */
  190. int irq;
  191. struct gpio_desc *pwdn_gpio;
  192. v4l2_std_id curr_norm;
  193. bool powered;
  194. bool streaming;
  195. u8 input;
  196. struct i2c_client *client;
  197. unsigned int register_page;
  198. struct i2c_client *csi_client;
  199. struct i2c_client *vpp_client;
  200. const struct adv7180_chip_info *chip_info;
  201. enum v4l2_field field;
  202. };
  203. #define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler, \
  204. struct adv7180_state, \
  205. ctrl_hdl)->sd)
  206. static int adv7180_select_page(struct adv7180_state *state, unsigned int page)
  207. {
  208. if (state->register_page != page) {
  209. i2c_smbus_write_byte_data(state->client, ADV7180_REG_CTRL,
  210. page);
  211. state->register_page = page;
  212. }
  213. return 0;
  214. }
  215. static int adv7180_write(struct adv7180_state *state, unsigned int reg,
  216. unsigned int value)
  217. {
  218. lockdep_assert_held(&state->mutex);
  219. adv7180_select_page(state, reg >> 8);
  220. return i2c_smbus_write_byte_data(state->client, reg & 0xff, value);
  221. }
  222. static int adv7180_read(struct adv7180_state *state, unsigned int reg)
  223. {
  224. lockdep_assert_held(&state->mutex);
  225. adv7180_select_page(state, reg >> 8);
  226. return i2c_smbus_read_byte_data(state->client, reg & 0xff);
  227. }
  228. static int adv7180_csi_write(struct adv7180_state *state, unsigned int reg,
  229. unsigned int value)
  230. {
  231. return i2c_smbus_write_byte_data(state->csi_client, reg, value);
  232. }
  233. static int adv7180_set_video_standard(struct adv7180_state *state,
  234. unsigned int std)
  235. {
  236. return state->chip_info->set_std(state, std);
  237. }
  238. static int adv7180_vpp_write(struct adv7180_state *state, unsigned int reg,
  239. unsigned int value)
  240. {
  241. return i2c_smbus_write_byte_data(state->vpp_client, reg, value);
  242. }
  243. static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
  244. {
  245. /* in case V4L2_IN_ST_NO_SIGNAL */
  246. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  247. return V4L2_STD_UNKNOWN;
  248. switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
  249. case ADV7180_STATUS1_AUTOD_NTSM_M_J:
  250. return V4L2_STD_NTSC;
  251. case ADV7180_STATUS1_AUTOD_NTSC_4_43:
  252. return V4L2_STD_NTSC_443;
  253. case ADV7180_STATUS1_AUTOD_PAL_M:
  254. return V4L2_STD_PAL_M;
  255. case ADV7180_STATUS1_AUTOD_PAL_60:
  256. return V4L2_STD_PAL_60;
  257. case ADV7180_STATUS1_AUTOD_PAL_B_G:
  258. return V4L2_STD_PAL;
  259. case ADV7180_STATUS1_AUTOD_SECAM:
  260. return V4L2_STD_SECAM;
  261. case ADV7180_STATUS1_AUTOD_PAL_COMB:
  262. return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  263. case ADV7180_STATUS1_AUTOD_SECAM_525:
  264. return V4L2_STD_SECAM;
  265. default:
  266. return V4L2_STD_UNKNOWN;
  267. }
  268. }
  269. static int v4l2_std_to_adv7180(v4l2_std_id std)
  270. {
  271. if (std == V4L2_STD_PAL_60)
  272. return ADV7180_STD_PAL60;
  273. if (std == V4L2_STD_NTSC_443)
  274. return ADV7180_STD_NTSC_443;
  275. if (std == V4L2_STD_PAL_N)
  276. return ADV7180_STD_PAL_N;
  277. if (std == V4L2_STD_PAL_M)
  278. return ADV7180_STD_PAL_M;
  279. if (std == V4L2_STD_PAL_Nc)
  280. return ADV7180_STD_PAL_COMB_N;
  281. if (std & V4L2_STD_PAL)
  282. return ADV7180_STD_PAL_BG;
  283. if (std & V4L2_STD_NTSC)
  284. return ADV7180_STD_NTSC_M;
  285. if (std & V4L2_STD_SECAM)
  286. return ADV7180_STD_PAL_SECAM;
  287. return -EINVAL;
  288. }
  289. static u32 adv7180_status_to_v4l2(u8 status1)
  290. {
  291. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  292. return V4L2_IN_ST_NO_SIGNAL;
  293. return 0;
  294. }
  295. static int __adv7180_status(struct adv7180_state *state, u32 *status,
  296. v4l2_std_id *std)
  297. {
  298. int status1 = adv7180_read(state, ADV7180_REG_STATUS1);
  299. if (status1 < 0)
  300. return status1;
  301. if (status)
  302. *status = adv7180_status_to_v4l2(status1);
  303. if (std)
  304. *std = adv7180_std_to_v4l2(status1);
  305. return 0;
  306. }
  307. static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
  308. {
  309. return container_of(sd, struct adv7180_state, sd);
  310. }
  311. static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  312. {
  313. struct adv7180_state *state = to_state(sd);
  314. int err = mutex_lock_interruptible(&state->mutex);
  315. if (err)
  316. return err;
  317. if (state->streaming) {
  318. err = -EBUSY;
  319. goto unlock;
  320. }
  321. err = adv7180_set_video_standard(state,
  322. ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM);
  323. if (err)
  324. goto unlock;
  325. msleep(100);
  326. __adv7180_status(state, NULL, std);
  327. err = v4l2_std_to_adv7180(state->curr_norm);
  328. if (err < 0)
  329. goto unlock;
  330. err = adv7180_set_video_standard(state, err);
  331. unlock:
  332. mutex_unlock(&state->mutex);
  333. return err;
  334. }
  335. static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
  336. u32 output, u32 config)
  337. {
  338. struct adv7180_state *state = to_state(sd);
  339. int ret = mutex_lock_interruptible(&state->mutex);
  340. if (ret)
  341. return ret;
  342. if (input > 31 || !(BIT(input) & state->chip_info->valid_input_mask)) {
  343. ret = -EINVAL;
  344. goto out;
  345. }
  346. ret = state->chip_info->select_input(state, input);
  347. if (ret == 0)
  348. state->input = input;
  349. out:
  350. mutex_unlock(&state->mutex);
  351. return ret;
  352. }
  353. static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
  354. {
  355. struct adv7180_state *state = to_state(sd);
  356. int ret = mutex_lock_interruptible(&state->mutex);
  357. if (ret)
  358. return ret;
  359. ret = __adv7180_status(state, status, NULL);
  360. mutex_unlock(&state->mutex);
  361. return ret;
  362. }
  363. static int adv7180_program_std(struct adv7180_state *state)
  364. {
  365. int ret;
  366. ret = v4l2_std_to_adv7180(state->curr_norm);
  367. if (ret < 0)
  368. return ret;
  369. ret = adv7180_set_video_standard(state, ret);
  370. if (ret < 0)
  371. return ret;
  372. return 0;
  373. }
  374. static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  375. {
  376. struct adv7180_state *state = to_state(sd);
  377. int ret = mutex_lock_interruptible(&state->mutex);
  378. if (ret)
  379. return ret;
  380. /* Make sure we can support this std */
  381. ret = v4l2_std_to_adv7180(std);
  382. if (ret < 0)
  383. goto out;
  384. state->curr_norm = std;
  385. ret = adv7180_program_std(state);
  386. out:
  387. mutex_unlock(&state->mutex);
  388. return ret;
  389. }
  390. static int adv7180_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
  391. {
  392. struct adv7180_state *state = to_state(sd);
  393. *norm = state->curr_norm;
  394. return 0;
  395. }
  396. static void adv7180_set_power_pin(struct adv7180_state *state, bool on)
  397. {
  398. if (!state->pwdn_gpio)
  399. return;
  400. if (on) {
  401. gpiod_set_value_cansleep(state->pwdn_gpio, 0);
  402. usleep_range(5000, 10000);
  403. } else {
  404. gpiod_set_value_cansleep(state->pwdn_gpio, 1);
  405. }
  406. }
  407. static int adv7180_set_power(struct adv7180_state *state, bool on)
  408. {
  409. u8 val;
  410. int ret;
  411. if (on)
  412. val = ADV7180_PWR_MAN_ON;
  413. else
  414. val = ADV7180_PWR_MAN_OFF;
  415. ret = adv7180_write(state, ADV7180_REG_PWR_MAN, val);
  416. if (ret)
  417. return ret;
  418. if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
  419. if (on) {
  420. adv7180_csi_write(state, 0xDE, 0x02);
  421. adv7180_csi_write(state, 0xD2, 0xF7);
  422. adv7180_csi_write(state, 0xD8, 0x65);
  423. adv7180_csi_write(state, 0xE0, 0x09);
  424. adv7180_csi_write(state, 0x2C, 0x00);
  425. if (state->field == V4L2_FIELD_NONE)
  426. adv7180_csi_write(state, 0x1D, 0x80);
  427. adv7180_csi_write(state, 0x00, 0x00);
  428. } else {
  429. adv7180_csi_write(state, 0x00, 0x80);
  430. }
  431. }
  432. return 0;
  433. }
  434. static int adv7180_s_power(struct v4l2_subdev *sd, int on)
  435. {
  436. struct adv7180_state *state = to_state(sd);
  437. int ret;
  438. ret = mutex_lock_interruptible(&state->mutex);
  439. if (ret)
  440. return ret;
  441. ret = adv7180_set_power(state, on);
  442. if (ret == 0)
  443. state->powered = on;
  444. mutex_unlock(&state->mutex);
  445. return ret;
  446. }
  447. static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
  448. {
  449. struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
  450. struct adv7180_state *state = to_state(sd);
  451. int ret = mutex_lock_interruptible(&state->mutex);
  452. int val;
  453. if (ret)
  454. return ret;
  455. val = ctrl->val;
  456. switch (ctrl->id) {
  457. case V4L2_CID_BRIGHTNESS:
  458. ret = adv7180_write(state, ADV7180_REG_BRI, val);
  459. break;
  460. case V4L2_CID_HUE:
  461. /*Hue is inverted according to HSL chart */
  462. ret = adv7180_write(state, ADV7180_REG_HUE, -val);
  463. break;
  464. case V4L2_CID_CONTRAST:
  465. ret = adv7180_write(state, ADV7180_REG_CON, val);
  466. break;
  467. case V4L2_CID_SATURATION:
  468. /*
  469. *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
  470. *Let's not confuse the user, everybody understands saturation
  471. */
  472. ret = adv7180_write(state, ADV7180_REG_SD_SAT_CB, val);
  473. if (ret < 0)
  474. break;
  475. ret = adv7180_write(state, ADV7180_REG_SD_SAT_CR, val);
  476. break;
  477. case V4L2_CID_ADV_FAST_SWITCH:
  478. if (ctrl->val) {
  479. /* ADI required write */
  480. adv7180_write(state, 0x80d9, 0x44);
  481. adv7180_write(state, ADV7180_REG_FLCONTROL,
  482. ADV7180_FLCONTROL_FL_ENABLE);
  483. } else {
  484. /* ADI required write */
  485. adv7180_write(state, 0x80d9, 0xc4);
  486. adv7180_write(state, ADV7180_REG_FLCONTROL, 0x00);
  487. }
  488. break;
  489. default:
  490. ret = -EINVAL;
  491. }
  492. mutex_unlock(&state->mutex);
  493. return ret;
  494. }
  495. static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
  496. .s_ctrl = adv7180_s_ctrl,
  497. };
  498. static const struct v4l2_ctrl_config adv7180_ctrl_fast_switch = {
  499. .ops = &adv7180_ctrl_ops,
  500. .id = V4L2_CID_ADV_FAST_SWITCH,
  501. .name = "Fast Switching",
  502. .type = V4L2_CTRL_TYPE_BOOLEAN,
  503. .min = 0,
  504. .max = 1,
  505. .step = 1,
  506. };
  507. static int adv7180_init_controls(struct adv7180_state *state)
  508. {
  509. v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
  510. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  511. V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
  512. ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
  513. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  514. V4L2_CID_CONTRAST, ADV7180_CON_MIN,
  515. ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
  516. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  517. V4L2_CID_SATURATION, ADV7180_SAT_MIN,
  518. ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
  519. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  520. V4L2_CID_HUE, ADV7180_HUE_MIN,
  521. ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
  522. v4l2_ctrl_new_custom(&state->ctrl_hdl, &adv7180_ctrl_fast_switch, NULL);
  523. state->sd.ctrl_handler = &state->ctrl_hdl;
  524. if (state->ctrl_hdl.error) {
  525. int err = state->ctrl_hdl.error;
  526. v4l2_ctrl_handler_free(&state->ctrl_hdl);
  527. return err;
  528. }
  529. v4l2_ctrl_handler_setup(&state->ctrl_hdl);
  530. return 0;
  531. }
  532. static void adv7180_exit_controls(struct adv7180_state *state)
  533. {
  534. v4l2_ctrl_handler_free(&state->ctrl_hdl);
  535. }
  536. static int adv7180_enum_mbus_code(struct v4l2_subdev *sd,
  537. struct v4l2_subdev_pad_config *cfg,
  538. struct v4l2_subdev_mbus_code_enum *code)
  539. {
  540. if (code->index != 0)
  541. return -EINVAL;
  542. code->code = MEDIA_BUS_FMT_UYVY8_2X8;
  543. return 0;
  544. }
  545. static int adv7180_mbus_fmt(struct v4l2_subdev *sd,
  546. struct v4l2_mbus_framefmt *fmt)
  547. {
  548. struct adv7180_state *state = to_state(sd);
  549. fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
  550. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  551. fmt->width = 720;
  552. fmt->height = state->curr_norm & V4L2_STD_525_60 ? 480 : 576;
  553. return 0;
  554. }
  555. static int adv7180_set_field_mode(struct adv7180_state *state)
  556. {
  557. if (!(state->chip_info->flags & ADV7180_FLAG_I2P))
  558. return 0;
  559. if (state->field == V4L2_FIELD_NONE) {
  560. if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
  561. adv7180_csi_write(state, 0x01, 0x20);
  562. adv7180_csi_write(state, 0x02, 0x28);
  563. adv7180_csi_write(state, 0x03, 0x38);
  564. adv7180_csi_write(state, 0x04, 0x30);
  565. adv7180_csi_write(state, 0x05, 0x30);
  566. adv7180_csi_write(state, 0x06, 0x80);
  567. adv7180_csi_write(state, 0x07, 0x70);
  568. adv7180_csi_write(state, 0x08, 0x50);
  569. }
  570. adv7180_vpp_write(state, 0xa3, 0x00);
  571. adv7180_vpp_write(state, 0x5b, 0x00);
  572. adv7180_vpp_write(state, 0x55, 0x80);
  573. } else {
  574. if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
  575. adv7180_csi_write(state, 0x01, 0x18);
  576. adv7180_csi_write(state, 0x02, 0x18);
  577. adv7180_csi_write(state, 0x03, 0x30);
  578. adv7180_csi_write(state, 0x04, 0x20);
  579. adv7180_csi_write(state, 0x05, 0x28);
  580. adv7180_csi_write(state, 0x06, 0x40);
  581. adv7180_csi_write(state, 0x07, 0x58);
  582. adv7180_csi_write(state, 0x08, 0x30);
  583. }
  584. adv7180_vpp_write(state, 0xa3, 0x70);
  585. adv7180_vpp_write(state, 0x5b, 0x80);
  586. adv7180_vpp_write(state, 0x55, 0x00);
  587. }
  588. return 0;
  589. }
  590. static int adv7180_get_pad_format(struct v4l2_subdev *sd,
  591. struct v4l2_subdev_pad_config *cfg,
  592. struct v4l2_subdev_format *format)
  593. {
  594. struct adv7180_state *state = to_state(sd);
  595. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  596. format->format = *v4l2_subdev_get_try_format(sd, cfg, 0);
  597. } else {
  598. adv7180_mbus_fmt(sd, &format->format);
  599. format->format.field = state->field;
  600. }
  601. return 0;
  602. }
  603. static int adv7180_set_pad_format(struct v4l2_subdev *sd,
  604. struct v4l2_subdev_pad_config *cfg,
  605. struct v4l2_subdev_format *format)
  606. {
  607. struct adv7180_state *state = to_state(sd);
  608. struct v4l2_mbus_framefmt *framefmt;
  609. int ret;
  610. switch (format->format.field) {
  611. case V4L2_FIELD_NONE:
  612. if (!(state->chip_info->flags & ADV7180_FLAG_I2P))
  613. format->format.field = V4L2_FIELD_INTERLACED;
  614. break;
  615. default:
  616. format->format.field = V4L2_FIELD_INTERLACED;
  617. break;
  618. }
  619. ret = adv7180_mbus_fmt(sd, &format->format);
  620. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  621. if (state->field != format->format.field) {
  622. state->field = format->format.field;
  623. adv7180_set_power(state, false);
  624. adv7180_set_field_mode(state);
  625. adv7180_set_power(state, true);
  626. }
  627. } else {
  628. framefmt = v4l2_subdev_get_try_format(sd, cfg, 0);
  629. *framefmt = format->format;
  630. }
  631. return ret;
  632. }
  633. static int adv7180_g_mbus_config(struct v4l2_subdev *sd,
  634. struct v4l2_mbus_config *cfg)
  635. {
  636. struct adv7180_state *state = to_state(sd);
  637. if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
  638. cfg->type = V4L2_MBUS_CSI2;
  639. cfg->flags = V4L2_MBUS_CSI2_1_LANE |
  640. V4L2_MBUS_CSI2_CHANNEL_0 |
  641. V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  642. } else {
  643. /*
  644. * The ADV7180 sensor supports BT.601/656 output modes.
  645. * The BT.656 is default and not yet configurable by s/w.
  646. */
  647. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
  648. V4L2_MBUS_DATA_ACTIVE_HIGH;
  649. cfg->type = V4L2_MBUS_BT656;
  650. }
  651. return 0;
  652. }
  653. static int adv7180_g_pixelaspect(struct v4l2_subdev *sd, struct v4l2_fract *aspect)
  654. {
  655. struct adv7180_state *state = to_state(sd);
  656. if (state->curr_norm & V4L2_STD_525_60) {
  657. aspect->numerator = 11;
  658. aspect->denominator = 10;
  659. } else {
  660. aspect->numerator = 54;
  661. aspect->denominator = 59;
  662. }
  663. return 0;
  664. }
  665. static int adv7180_g_tvnorms(struct v4l2_subdev *sd, v4l2_std_id *norm)
  666. {
  667. *norm = V4L2_STD_ALL;
  668. return 0;
  669. }
  670. static int adv7180_s_stream(struct v4l2_subdev *sd, int enable)
  671. {
  672. struct adv7180_state *state = to_state(sd);
  673. int ret;
  674. /* It's always safe to stop streaming, no need to take the lock */
  675. if (!enable) {
  676. state->streaming = enable;
  677. return 0;
  678. }
  679. /* Must wait until querystd released the lock */
  680. ret = mutex_lock_interruptible(&state->mutex);
  681. if (ret)
  682. return ret;
  683. state->streaming = enable;
  684. mutex_unlock(&state->mutex);
  685. return 0;
  686. }
  687. static int adv7180_subscribe_event(struct v4l2_subdev *sd,
  688. struct v4l2_fh *fh,
  689. struct v4l2_event_subscription *sub)
  690. {
  691. switch (sub->type) {
  692. case V4L2_EVENT_SOURCE_CHANGE:
  693. return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
  694. case V4L2_EVENT_CTRL:
  695. return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
  696. default:
  697. return -EINVAL;
  698. }
  699. }
  700. static const struct v4l2_subdev_video_ops adv7180_video_ops = {
  701. .s_std = adv7180_s_std,
  702. .g_std = adv7180_g_std,
  703. .querystd = adv7180_querystd,
  704. .g_input_status = adv7180_g_input_status,
  705. .s_routing = adv7180_s_routing,
  706. .g_mbus_config = adv7180_g_mbus_config,
  707. .g_pixelaspect = adv7180_g_pixelaspect,
  708. .g_tvnorms = adv7180_g_tvnorms,
  709. .s_stream = adv7180_s_stream,
  710. };
  711. static const struct v4l2_subdev_core_ops adv7180_core_ops = {
  712. .s_power = adv7180_s_power,
  713. .subscribe_event = adv7180_subscribe_event,
  714. .unsubscribe_event = v4l2_event_subdev_unsubscribe,
  715. };
  716. static const struct v4l2_subdev_pad_ops adv7180_pad_ops = {
  717. .enum_mbus_code = adv7180_enum_mbus_code,
  718. .set_fmt = adv7180_set_pad_format,
  719. .get_fmt = adv7180_get_pad_format,
  720. };
  721. static const struct v4l2_subdev_ops adv7180_ops = {
  722. .core = &adv7180_core_ops,
  723. .video = &adv7180_video_ops,
  724. .pad = &adv7180_pad_ops,
  725. };
  726. static irqreturn_t adv7180_irq(int irq, void *devid)
  727. {
  728. struct adv7180_state *state = devid;
  729. u8 isr3;
  730. mutex_lock(&state->mutex);
  731. isr3 = adv7180_read(state, ADV7180_REG_ISR3);
  732. /* clear */
  733. adv7180_write(state, ADV7180_REG_ICR3, isr3);
  734. if (isr3 & ADV7180_IRQ3_AD_CHANGE) {
  735. static const struct v4l2_event src_ch = {
  736. .type = V4L2_EVENT_SOURCE_CHANGE,
  737. .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
  738. };
  739. v4l2_subdev_notify_event(&state->sd, &src_ch);
  740. }
  741. mutex_unlock(&state->mutex);
  742. return IRQ_HANDLED;
  743. }
  744. static int adv7180_init(struct adv7180_state *state)
  745. {
  746. int ret;
  747. /* ITU-R BT.656-4 compatible */
  748. ret = adv7180_write(state, ADV7180_REG_EXTENDED_OUTPUT_CONTROL,
  749. ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
  750. if (ret < 0)
  751. return ret;
  752. /* Manually set V bit end position in NTSC mode */
  753. return adv7180_write(state, ADV7180_REG_NTSC_V_BIT_END,
  754. ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
  755. }
  756. static int adv7180_set_std(struct adv7180_state *state, unsigned int std)
  757. {
  758. return adv7180_write(state, ADV7180_REG_INPUT_CONTROL,
  759. (std << 4) | state->input);
  760. }
  761. static int adv7180_select_input(struct adv7180_state *state, unsigned int input)
  762. {
  763. int ret;
  764. ret = adv7180_read(state, ADV7180_REG_INPUT_CONTROL);
  765. if (ret < 0)
  766. return ret;
  767. ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
  768. ret |= input;
  769. return adv7180_write(state, ADV7180_REG_INPUT_CONTROL, ret);
  770. }
  771. static int adv7182_init(struct adv7180_state *state)
  772. {
  773. if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2)
  774. adv7180_write(state, ADV7180_REG_CSI_SLAVE_ADDR,
  775. ADV7180_DEFAULT_CSI_I2C_ADDR << 1);
  776. if (state->chip_info->flags & ADV7180_FLAG_I2P)
  777. adv7180_write(state, ADV7180_REG_VPP_SLAVE_ADDR,
  778. ADV7180_DEFAULT_VPP_I2C_ADDR << 1);
  779. if (state->chip_info->flags & ADV7180_FLAG_V2) {
  780. /* ADI recommended writes for improved video quality */
  781. adv7180_write(state, 0x0080, 0x51);
  782. adv7180_write(state, 0x0081, 0x51);
  783. adv7180_write(state, 0x0082, 0x68);
  784. }
  785. /* ADI required writes */
  786. if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
  787. adv7180_write(state, ADV7180_REG_OUTPUT_CONTROL, 0x4e);
  788. adv7180_write(state, ADV7180_REG_EXTENDED_OUTPUT_CONTROL, 0x57);
  789. adv7180_write(state, ADV7180_REG_CTRL_2, 0xc0);
  790. } else {
  791. if (state->chip_info->flags & ADV7180_FLAG_V2)
  792. adv7180_write(state,
  793. ADV7180_REG_EXTENDED_OUTPUT_CONTROL,
  794. 0x17);
  795. else
  796. adv7180_write(state,
  797. ADV7180_REG_EXTENDED_OUTPUT_CONTROL,
  798. 0x07);
  799. adv7180_write(state, ADV7180_REG_OUTPUT_CONTROL, 0x0c);
  800. adv7180_write(state, ADV7180_REG_CTRL_2, 0x40);
  801. }
  802. adv7180_write(state, 0x0013, 0x00);
  803. return 0;
  804. }
  805. static int adv7182_set_std(struct adv7180_state *state, unsigned int std)
  806. {
  807. return adv7180_write(state, ADV7182_REG_INPUT_VIDSEL, std << 4);
  808. }
  809. enum adv7182_input_type {
  810. ADV7182_INPUT_TYPE_CVBS,
  811. ADV7182_INPUT_TYPE_DIFF_CVBS,
  812. ADV7182_INPUT_TYPE_SVIDEO,
  813. ADV7182_INPUT_TYPE_YPBPR,
  814. };
  815. static enum adv7182_input_type adv7182_get_input_type(unsigned int input)
  816. {
  817. switch (input) {
  818. case ADV7182_INPUT_CVBS_AIN1:
  819. case ADV7182_INPUT_CVBS_AIN2:
  820. case ADV7182_INPUT_CVBS_AIN3:
  821. case ADV7182_INPUT_CVBS_AIN4:
  822. case ADV7182_INPUT_CVBS_AIN5:
  823. case ADV7182_INPUT_CVBS_AIN6:
  824. case ADV7182_INPUT_CVBS_AIN7:
  825. case ADV7182_INPUT_CVBS_AIN8:
  826. return ADV7182_INPUT_TYPE_CVBS;
  827. case ADV7182_INPUT_SVIDEO_AIN1_AIN2:
  828. case ADV7182_INPUT_SVIDEO_AIN3_AIN4:
  829. case ADV7182_INPUT_SVIDEO_AIN5_AIN6:
  830. case ADV7182_INPUT_SVIDEO_AIN7_AIN8:
  831. return ADV7182_INPUT_TYPE_SVIDEO;
  832. case ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3:
  833. case ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6:
  834. return ADV7182_INPUT_TYPE_YPBPR;
  835. case ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2:
  836. case ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4:
  837. case ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6:
  838. case ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8:
  839. return ADV7182_INPUT_TYPE_DIFF_CVBS;
  840. default: /* Will never happen */
  841. return 0;
  842. }
  843. }
  844. /* ADI recommended writes to registers 0x52, 0x53, 0x54 */
  845. static unsigned int adv7182_lbias_settings[][3] = {
  846. [ADV7182_INPUT_TYPE_CVBS] = { 0xCB, 0x4E, 0x80 },
  847. [ADV7182_INPUT_TYPE_DIFF_CVBS] = { 0xC0, 0x4E, 0x80 },
  848. [ADV7182_INPUT_TYPE_SVIDEO] = { 0x0B, 0xCE, 0x80 },
  849. [ADV7182_INPUT_TYPE_YPBPR] = { 0x0B, 0x4E, 0xC0 },
  850. };
  851. static unsigned int adv7280_lbias_settings[][3] = {
  852. [ADV7182_INPUT_TYPE_CVBS] = { 0xCD, 0x4E, 0x80 },
  853. [ADV7182_INPUT_TYPE_DIFF_CVBS] = { 0xC0, 0x4E, 0x80 },
  854. [ADV7182_INPUT_TYPE_SVIDEO] = { 0x0B, 0xCE, 0x80 },
  855. [ADV7182_INPUT_TYPE_YPBPR] = { 0x0B, 0x4E, 0xC0 },
  856. };
  857. static int adv7182_select_input(struct adv7180_state *state, unsigned int input)
  858. {
  859. enum adv7182_input_type input_type;
  860. unsigned int *lbias;
  861. unsigned int i;
  862. int ret;
  863. ret = adv7180_write(state, ADV7180_REG_INPUT_CONTROL, input);
  864. if (ret)
  865. return ret;
  866. /* Reset clamp circuitry - ADI recommended writes */
  867. adv7180_write(state, ADV7180_REG_RST_CLAMP, 0x00);
  868. adv7180_write(state, ADV7180_REG_RST_CLAMP, 0xff);
  869. input_type = adv7182_get_input_type(input);
  870. switch (input_type) {
  871. case ADV7182_INPUT_TYPE_CVBS:
  872. case ADV7182_INPUT_TYPE_DIFF_CVBS:
  873. /* ADI recommends to use the SH1 filter */
  874. adv7180_write(state, ADV7180_REG_SHAP_FILTER_CTL_1, 0x41);
  875. break;
  876. default:
  877. adv7180_write(state, ADV7180_REG_SHAP_FILTER_CTL_1, 0x01);
  878. break;
  879. }
  880. if (state->chip_info->flags & ADV7180_FLAG_V2)
  881. lbias = adv7280_lbias_settings[input_type];
  882. else
  883. lbias = adv7182_lbias_settings[input_type];
  884. for (i = 0; i < ARRAY_SIZE(adv7182_lbias_settings[0]); i++)
  885. adv7180_write(state, ADV7180_REG_CVBS_TRIM + i, lbias[i]);
  886. if (input_type == ADV7182_INPUT_TYPE_DIFF_CVBS) {
  887. /* ADI required writes to make differential CVBS work */
  888. adv7180_write(state, ADV7180_REG_RES_CIR, 0xa8);
  889. adv7180_write(state, ADV7180_REG_CLAMP_ADJ, 0x90);
  890. adv7180_write(state, ADV7180_REG_DIFF_MODE, 0xb0);
  891. adv7180_write(state, ADV7180_REG_AGC_ADJ1, 0x08);
  892. adv7180_write(state, ADV7180_REG_AGC_ADJ2, 0xa0);
  893. } else {
  894. adv7180_write(state, ADV7180_REG_RES_CIR, 0xf0);
  895. adv7180_write(state, ADV7180_REG_CLAMP_ADJ, 0xd0);
  896. adv7180_write(state, ADV7180_REG_DIFF_MODE, 0x10);
  897. adv7180_write(state, ADV7180_REG_AGC_ADJ1, 0x9c);
  898. adv7180_write(state, ADV7180_REG_AGC_ADJ2, 0x00);
  899. }
  900. return 0;
  901. }
  902. static const struct adv7180_chip_info adv7180_info = {
  903. .flags = ADV7180_FLAG_RESET_POWERED,
  904. /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
  905. * all inputs and let the card driver take care of validation
  906. */
  907. .valid_input_mask = BIT(ADV7180_INPUT_CVBS_AIN1) |
  908. BIT(ADV7180_INPUT_CVBS_AIN2) |
  909. BIT(ADV7180_INPUT_CVBS_AIN3) |
  910. BIT(ADV7180_INPUT_CVBS_AIN4) |
  911. BIT(ADV7180_INPUT_CVBS_AIN5) |
  912. BIT(ADV7180_INPUT_CVBS_AIN6) |
  913. BIT(ADV7180_INPUT_SVIDEO_AIN1_AIN2) |
  914. BIT(ADV7180_INPUT_SVIDEO_AIN3_AIN4) |
  915. BIT(ADV7180_INPUT_SVIDEO_AIN5_AIN6) |
  916. BIT(ADV7180_INPUT_YPRPB_AIN1_AIN2_AIN3) |
  917. BIT(ADV7180_INPUT_YPRPB_AIN4_AIN5_AIN6),
  918. .init = adv7180_init,
  919. .set_std = adv7180_set_std,
  920. .select_input = adv7180_select_input,
  921. };
  922. static const struct adv7180_chip_info adv7182_info = {
  923. .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
  924. BIT(ADV7182_INPUT_CVBS_AIN2) |
  925. BIT(ADV7182_INPUT_CVBS_AIN3) |
  926. BIT(ADV7182_INPUT_CVBS_AIN4) |
  927. BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
  928. BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
  929. BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
  930. BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
  931. BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4),
  932. .init = adv7182_init,
  933. .set_std = adv7182_set_std,
  934. .select_input = adv7182_select_input,
  935. };
  936. static const struct adv7180_chip_info adv7280_info = {
  937. .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_I2P,
  938. .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
  939. BIT(ADV7182_INPUT_CVBS_AIN2) |
  940. BIT(ADV7182_INPUT_CVBS_AIN3) |
  941. BIT(ADV7182_INPUT_CVBS_AIN4) |
  942. BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
  943. BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
  944. BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3),
  945. .init = adv7182_init,
  946. .set_std = adv7182_set_std,
  947. .select_input = adv7182_select_input,
  948. };
  949. static const struct adv7180_chip_info adv7280_m_info = {
  950. .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2 | ADV7180_FLAG_I2P,
  951. .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
  952. BIT(ADV7182_INPUT_CVBS_AIN2) |
  953. BIT(ADV7182_INPUT_CVBS_AIN3) |
  954. BIT(ADV7182_INPUT_CVBS_AIN4) |
  955. BIT(ADV7182_INPUT_CVBS_AIN5) |
  956. BIT(ADV7182_INPUT_CVBS_AIN6) |
  957. BIT(ADV7182_INPUT_CVBS_AIN7) |
  958. BIT(ADV7182_INPUT_CVBS_AIN8) |
  959. BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
  960. BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
  961. BIT(ADV7182_INPUT_SVIDEO_AIN5_AIN6) |
  962. BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
  963. BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
  964. BIT(ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6),
  965. .init = adv7182_init,
  966. .set_std = adv7182_set_std,
  967. .select_input = adv7182_select_input,
  968. };
  969. static const struct adv7180_chip_info adv7281_info = {
  970. .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2,
  971. .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
  972. BIT(ADV7182_INPUT_CVBS_AIN2) |
  973. BIT(ADV7182_INPUT_CVBS_AIN7) |
  974. BIT(ADV7182_INPUT_CVBS_AIN8) |
  975. BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
  976. BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
  977. BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
  978. BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
  979. .init = adv7182_init,
  980. .set_std = adv7182_set_std,
  981. .select_input = adv7182_select_input,
  982. };
  983. static const struct adv7180_chip_info adv7281_m_info = {
  984. .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2,
  985. .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
  986. BIT(ADV7182_INPUT_CVBS_AIN2) |
  987. BIT(ADV7182_INPUT_CVBS_AIN3) |
  988. BIT(ADV7182_INPUT_CVBS_AIN4) |
  989. BIT(ADV7182_INPUT_CVBS_AIN7) |
  990. BIT(ADV7182_INPUT_CVBS_AIN8) |
  991. BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
  992. BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
  993. BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
  994. BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
  995. BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
  996. BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4) |
  997. BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
  998. .init = adv7182_init,
  999. .set_std = adv7182_set_std,
  1000. .select_input = adv7182_select_input,
  1001. };
  1002. static const struct adv7180_chip_info adv7281_ma_info = {
  1003. .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2,
  1004. .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
  1005. BIT(ADV7182_INPUT_CVBS_AIN2) |
  1006. BIT(ADV7182_INPUT_CVBS_AIN3) |
  1007. BIT(ADV7182_INPUT_CVBS_AIN4) |
  1008. BIT(ADV7182_INPUT_CVBS_AIN5) |
  1009. BIT(ADV7182_INPUT_CVBS_AIN6) |
  1010. BIT(ADV7182_INPUT_CVBS_AIN7) |
  1011. BIT(ADV7182_INPUT_CVBS_AIN8) |
  1012. BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
  1013. BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
  1014. BIT(ADV7182_INPUT_SVIDEO_AIN5_AIN6) |
  1015. BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
  1016. BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
  1017. BIT(ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6) |
  1018. BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
  1019. BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4) |
  1020. BIT(ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6) |
  1021. BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
  1022. .init = adv7182_init,
  1023. .set_std = adv7182_set_std,
  1024. .select_input = adv7182_select_input,
  1025. };
  1026. static const struct adv7180_chip_info adv7282_info = {
  1027. .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_I2P,
  1028. .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
  1029. BIT(ADV7182_INPUT_CVBS_AIN2) |
  1030. BIT(ADV7182_INPUT_CVBS_AIN7) |
  1031. BIT(ADV7182_INPUT_CVBS_AIN8) |
  1032. BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
  1033. BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
  1034. BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
  1035. BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
  1036. .init = adv7182_init,
  1037. .set_std = adv7182_set_std,
  1038. .select_input = adv7182_select_input,
  1039. };
  1040. static const struct adv7180_chip_info adv7282_m_info = {
  1041. .flags = ADV7180_FLAG_V2 | ADV7180_FLAG_MIPI_CSI2 | ADV7180_FLAG_I2P,
  1042. .valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
  1043. BIT(ADV7182_INPUT_CVBS_AIN2) |
  1044. BIT(ADV7182_INPUT_CVBS_AIN3) |
  1045. BIT(ADV7182_INPUT_CVBS_AIN4) |
  1046. BIT(ADV7182_INPUT_CVBS_AIN7) |
  1047. BIT(ADV7182_INPUT_CVBS_AIN8) |
  1048. BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
  1049. BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
  1050. BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
  1051. BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
  1052. BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4) |
  1053. BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
  1054. .init = adv7182_init,
  1055. .set_std = adv7182_set_std,
  1056. .select_input = adv7182_select_input,
  1057. };
  1058. static int init_device(struct adv7180_state *state)
  1059. {
  1060. int ret;
  1061. mutex_lock(&state->mutex);
  1062. adv7180_set_power_pin(state, true);
  1063. adv7180_write(state, ADV7180_REG_PWR_MAN, ADV7180_PWR_MAN_RES);
  1064. usleep_range(5000, 10000);
  1065. ret = state->chip_info->init(state);
  1066. if (ret)
  1067. goto out_unlock;
  1068. ret = adv7180_program_std(state);
  1069. if (ret)
  1070. goto out_unlock;
  1071. adv7180_set_field_mode(state);
  1072. /* register for interrupts */
  1073. if (state->irq > 0) {
  1074. /* config the Interrupt pin to be active low */
  1075. ret = adv7180_write(state, ADV7180_REG_ICONF1,
  1076. ADV7180_ICONF1_ACTIVE_LOW |
  1077. ADV7180_ICONF1_PSYNC_ONLY);
  1078. if (ret < 0)
  1079. goto out_unlock;
  1080. ret = adv7180_write(state, ADV7180_REG_IMR1, 0);
  1081. if (ret < 0)
  1082. goto out_unlock;
  1083. ret = adv7180_write(state, ADV7180_REG_IMR2, 0);
  1084. if (ret < 0)
  1085. goto out_unlock;
  1086. /* enable AD change interrupts interrupts */
  1087. ret = adv7180_write(state, ADV7180_REG_IMR3,
  1088. ADV7180_IRQ3_AD_CHANGE);
  1089. if (ret < 0)
  1090. goto out_unlock;
  1091. ret = adv7180_write(state, ADV7180_REG_IMR4, 0);
  1092. if (ret < 0)
  1093. goto out_unlock;
  1094. }
  1095. out_unlock:
  1096. mutex_unlock(&state->mutex);
  1097. return ret;
  1098. }
  1099. static int adv7180_probe(struct i2c_client *client,
  1100. const struct i2c_device_id *id)
  1101. {
  1102. struct adv7180_state *state;
  1103. struct v4l2_subdev *sd;
  1104. int ret;
  1105. /* Check if the adapter supports the needed features */
  1106. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1107. return -EIO;
  1108. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  1109. client->addr, client->adapter->name);
  1110. state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
  1111. if (state == NULL)
  1112. return -ENOMEM;
  1113. state->client = client;
  1114. state->field = V4L2_FIELD_INTERLACED;
  1115. state->chip_info = (struct adv7180_chip_info *)id->driver_data;
  1116. state->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
  1117. GPIOD_OUT_HIGH);
  1118. if (IS_ERR(state->pwdn_gpio)) {
  1119. ret = PTR_ERR(state->pwdn_gpio);
  1120. v4l_err(client, "request for power pin failed: %d\n", ret);
  1121. return ret;
  1122. }
  1123. if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2) {
  1124. state->csi_client = i2c_new_dummy(client->adapter,
  1125. ADV7180_DEFAULT_CSI_I2C_ADDR);
  1126. if (!state->csi_client)
  1127. return -ENOMEM;
  1128. }
  1129. if (state->chip_info->flags & ADV7180_FLAG_I2P) {
  1130. state->vpp_client = i2c_new_dummy(client->adapter,
  1131. ADV7180_DEFAULT_VPP_I2C_ADDR);
  1132. if (!state->vpp_client) {
  1133. ret = -ENOMEM;
  1134. goto err_unregister_csi_client;
  1135. }
  1136. }
  1137. state->irq = client->irq;
  1138. mutex_init(&state->mutex);
  1139. state->curr_norm = V4L2_STD_NTSC;
  1140. if (state->chip_info->flags & ADV7180_FLAG_RESET_POWERED)
  1141. state->powered = true;
  1142. else
  1143. state->powered = false;
  1144. state->input = 0;
  1145. sd = &state->sd;
  1146. v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
  1147. sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1148. ret = adv7180_init_controls(state);
  1149. if (ret)
  1150. goto err_unregister_vpp_client;
  1151. state->pad.flags = MEDIA_PAD_FL_SOURCE;
  1152. sd->entity.flags |= MEDIA_ENT_F_ATV_DECODER;
  1153. ret = media_entity_pads_init(&sd->entity, 1, &state->pad);
  1154. if (ret)
  1155. goto err_free_ctrl;
  1156. ret = init_device(state);
  1157. if (ret)
  1158. goto err_media_entity_cleanup;
  1159. if (state->irq) {
  1160. ret = request_threaded_irq(client->irq, NULL, adv7180_irq,
  1161. IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
  1162. KBUILD_MODNAME, state);
  1163. if (ret)
  1164. goto err_media_entity_cleanup;
  1165. }
  1166. ret = v4l2_async_register_subdev(sd);
  1167. if (ret)
  1168. goto err_free_irq;
  1169. return 0;
  1170. err_free_irq:
  1171. if (state->irq > 0)
  1172. free_irq(client->irq, state);
  1173. err_media_entity_cleanup:
  1174. media_entity_cleanup(&sd->entity);
  1175. err_free_ctrl:
  1176. adv7180_exit_controls(state);
  1177. err_unregister_vpp_client:
  1178. if (state->chip_info->flags & ADV7180_FLAG_I2P)
  1179. i2c_unregister_device(state->vpp_client);
  1180. err_unregister_csi_client:
  1181. if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2)
  1182. i2c_unregister_device(state->csi_client);
  1183. mutex_destroy(&state->mutex);
  1184. return ret;
  1185. }
  1186. static int adv7180_remove(struct i2c_client *client)
  1187. {
  1188. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1189. struct adv7180_state *state = to_state(sd);
  1190. v4l2_async_unregister_subdev(sd);
  1191. if (state->irq > 0)
  1192. free_irq(client->irq, state);
  1193. media_entity_cleanup(&sd->entity);
  1194. adv7180_exit_controls(state);
  1195. if (state->chip_info->flags & ADV7180_FLAG_I2P)
  1196. i2c_unregister_device(state->vpp_client);
  1197. if (state->chip_info->flags & ADV7180_FLAG_MIPI_CSI2)
  1198. i2c_unregister_device(state->csi_client);
  1199. adv7180_set_power_pin(state, false);
  1200. mutex_destroy(&state->mutex);
  1201. return 0;
  1202. }
  1203. static const struct i2c_device_id adv7180_id[] = {
  1204. { "adv7180", (kernel_ulong_t)&adv7180_info },
  1205. { "adv7182", (kernel_ulong_t)&adv7182_info },
  1206. { "adv7280", (kernel_ulong_t)&adv7280_info },
  1207. { "adv7280-m", (kernel_ulong_t)&adv7280_m_info },
  1208. { "adv7281", (kernel_ulong_t)&adv7281_info },
  1209. { "adv7281-m", (kernel_ulong_t)&adv7281_m_info },
  1210. { "adv7281-ma", (kernel_ulong_t)&adv7281_ma_info },
  1211. { "adv7282", (kernel_ulong_t)&adv7282_info },
  1212. { "adv7282-m", (kernel_ulong_t)&adv7282_m_info },
  1213. {},
  1214. };
  1215. MODULE_DEVICE_TABLE(i2c, adv7180_id);
  1216. #ifdef CONFIG_PM_SLEEP
  1217. static int adv7180_suspend(struct device *dev)
  1218. {
  1219. struct i2c_client *client = to_i2c_client(dev);
  1220. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1221. struct adv7180_state *state = to_state(sd);
  1222. return adv7180_set_power(state, false);
  1223. }
  1224. static int adv7180_resume(struct device *dev)
  1225. {
  1226. struct i2c_client *client = to_i2c_client(dev);
  1227. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1228. struct adv7180_state *state = to_state(sd);
  1229. int ret;
  1230. ret = init_device(state);
  1231. if (ret < 0)
  1232. return ret;
  1233. ret = adv7180_set_power(state, state->powered);
  1234. if (ret)
  1235. return ret;
  1236. return 0;
  1237. }
  1238. static SIMPLE_DEV_PM_OPS(adv7180_pm_ops, adv7180_suspend, adv7180_resume);
  1239. #define ADV7180_PM_OPS (&adv7180_pm_ops)
  1240. #else
  1241. #define ADV7180_PM_OPS NULL
  1242. #endif
  1243. #ifdef CONFIG_OF
  1244. static const struct of_device_id adv7180_of_id[] = {
  1245. { .compatible = "adi,adv7180", },
  1246. { .compatible = "adi,adv7182", },
  1247. { .compatible = "adi,adv7280", },
  1248. { .compatible = "adi,adv7280-m", },
  1249. { .compatible = "adi,adv7281", },
  1250. { .compatible = "adi,adv7281-m", },
  1251. { .compatible = "adi,adv7281-ma", },
  1252. { .compatible = "adi,adv7282", },
  1253. { .compatible = "adi,adv7282-m", },
  1254. { },
  1255. };
  1256. MODULE_DEVICE_TABLE(of, adv7180_of_id);
  1257. #endif
  1258. static struct i2c_driver adv7180_driver = {
  1259. .driver = {
  1260. .name = KBUILD_MODNAME,
  1261. .pm = ADV7180_PM_OPS,
  1262. .of_match_table = of_match_ptr(adv7180_of_id),
  1263. },
  1264. .probe = adv7180_probe,
  1265. .remove = adv7180_remove,
  1266. .id_table = adv7180_id,
  1267. };
  1268. module_i2c_driver(adv7180_driver);
  1269. MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
  1270. MODULE_AUTHOR("Mocean Laboratories");
  1271. MODULE_LICENSE("GPL v2");