adv7180.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * adv7180.c Analog Devices ADV7180 video decoder driver
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/i2c.h>
  24. #include <linux/slab.h>
  25. #include <media/v4l2-ioctl.h>
  26. #include <linux/videodev2.h>
  27. #include <media/v4l2-device.h>
  28. #include <media/v4l2-chip-ident.h>
  29. #include <linux/mutex.h>
  30. #define DRIVER_NAME "adv7180"
  31. #define ADV7180_INPUT_CONTROL_REG 0x00
  32. #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00
  33. #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
  34. #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20
  35. #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30
  36. #define ADV7180_INPUT_CONTROL_NTSC_J 0x40
  37. #define ADV7180_INPUT_CONTROL_NTSC_M 0x50
  38. #define ADV7180_INPUT_CONTROL_PAL60 0x60
  39. #define ADV7180_INPUT_CONTROL_NTSC_443 0x70
  40. #define ADV7180_INPUT_CONTROL_PAL_BG 0x80
  41. #define ADV7180_INPUT_CONTROL_PAL_N 0x90
  42. #define ADV7180_INPUT_CONTROL_PAL_M 0xa0
  43. #define ADV7180_INPUT_CONTROL_PAL_M_PED 0xb0
  44. #define ADV7180_INPUT_CONTROL_PAL_COMB_N 0xc0
  45. #define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED 0xd0
  46. #define ADV7180_INPUT_CONTROL_PAL_SECAM 0xe0
  47. #define ADV7180_INPUT_CONTROL_PAL_SECAM_PED 0xf0
  48. #define ADV7180_INPUT_CONTROL_INSEL_MASK 0x0f
  49. #define ADV7180_EXTENDED_OUTPUT_CONTROL_REG 0x04
  50. #define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5
  51. #define ADV7180_AUTODETECT_ENABLE_REG 0x07
  52. #define ADV7180_AUTODETECT_DEFAULT 0x7f
  53. #define ADV7180_CON_REG 0x08 /*Unsigned */
  54. #define CON_REG_MIN 0
  55. #define CON_REG_DEF 128
  56. #define CON_REG_MAX 255
  57. #define ADV7180_BRI_REG 0x0a /*Signed */
  58. #define BRI_REG_MIN -128
  59. #define BRI_REG_DEF 0
  60. #define BRI_REG_MAX 127
  61. #define ADV7180_HUE_REG 0x0b /*Signed, inverted */
  62. #define HUE_REG_MIN -127
  63. #define HUE_REG_DEF 0
  64. #define HUE_REG_MAX 128
  65. #define ADV7180_ADI_CTRL_REG 0x0e
  66. #define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
  67. #define ADV7180_PWR_MAN_REG 0x0f
  68. #define ADV7180_PWR_MAN_ON 0x04
  69. #define ADV7180_PWR_MAN_OFF 0x24
  70. #define ADV7180_PWR_MAN_RES 0x80
  71. #define ADV7180_STATUS1_REG 0x10
  72. #define ADV7180_STATUS1_IN_LOCK 0x01
  73. #define ADV7180_STATUS1_AUTOD_MASK 0x70
  74. #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
  75. #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
  76. #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
  77. #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
  78. #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
  79. #define ADV7180_STATUS1_AUTOD_SECAM 0x50
  80. #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
  81. #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
  82. #define ADV7180_IDENT_REG 0x11
  83. #define ADV7180_ID_7180 0x18
  84. #define ADV7180_ICONF1_ADI 0x40
  85. #define ADV7180_ICONF1_ACTIVE_LOW 0x01
  86. #define ADV7180_ICONF1_PSYNC_ONLY 0x10
  87. #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
  88. #define ADV7180_SD_SAT_CB_REG 0xe3 /*Unsigned */
  89. #define ADV7180_SD_SAT_CR_REG 0xe4 /*Unsigned */
  90. #define SAT_REG_MIN 0
  91. #define SAT_REG_DEF 128
  92. #define SAT_REG_MAX 255
  93. #define ADV7180_IRQ1_LOCK 0x01
  94. #define ADV7180_IRQ1_UNLOCK 0x02
  95. #define ADV7180_ISR1_ADI 0x42
  96. #define ADV7180_ICR1_ADI 0x43
  97. #define ADV7180_IMR1_ADI 0x44
  98. #define ADV7180_IMR2_ADI 0x48
  99. #define ADV7180_IRQ3_AD_CHANGE 0x08
  100. #define ADV7180_ISR3_ADI 0x4A
  101. #define ADV7180_ICR3_ADI 0x4B
  102. #define ADV7180_IMR3_ADI 0x4C
  103. #define ADV7180_IMR4_ADI 0x50
  104. #define ADV7180_NTSC_V_BIT_END_REG 0xE6
  105. #define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F
  106. struct adv7180_state {
  107. struct v4l2_subdev sd;
  108. struct work_struct work;
  109. struct mutex mutex; /* mutual excl. when accessing chip */
  110. int irq;
  111. v4l2_std_id curr_norm;
  112. bool autodetect;
  113. s8 brightness;
  114. s16 hue;
  115. u8 contrast;
  116. u8 saturation;
  117. u8 input;
  118. };
  119. static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
  120. {
  121. switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
  122. case ADV7180_STATUS1_AUTOD_NTSM_M_J:
  123. return V4L2_STD_NTSC;
  124. case ADV7180_STATUS1_AUTOD_NTSC_4_43:
  125. return V4L2_STD_NTSC_443;
  126. case ADV7180_STATUS1_AUTOD_PAL_M:
  127. return V4L2_STD_PAL_M;
  128. case ADV7180_STATUS1_AUTOD_PAL_60:
  129. return V4L2_STD_PAL_60;
  130. case ADV7180_STATUS1_AUTOD_PAL_B_G:
  131. return V4L2_STD_PAL;
  132. case ADV7180_STATUS1_AUTOD_SECAM:
  133. return V4L2_STD_SECAM;
  134. case ADV7180_STATUS1_AUTOD_PAL_COMB:
  135. return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  136. case ADV7180_STATUS1_AUTOD_SECAM_525:
  137. return V4L2_STD_SECAM;
  138. default:
  139. return V4L2_STD_UNKNOWN;
  140. }
  141. }
  142. static int v4l2_std_to_adv7180(v4l2_std_id std)
  143. {
  144. if (std == V4L2_STD_PAL_60)
  145. return ADV7180_INPUT_CONTROL_PAL60;
  146. if (std == V4L2_STD_NTSC_443)
  147. return ADV7180_INPUT_CONTROL_NTSC_443;
  148. if (std == V4L2_STD_PAL_N)
  149. return ADV7180_INPUT_CONTROL_PAL_N;
  150. if (std == V4L2_STD_PAL_M)
  151. return ADV7180_INPUT_CONTROL_PAL_M;
  152. if (std == V4L2_STD_PAL_Nc)
  153. return ADV7180_INPUT_CONTROL_PAL_COMB_N;
  154. if (std & V4L2_STD_PAL)
  155. return ADV7180_INPUT_CONTROL_PAL_BG;
  156. if (std & V4L2_STD_NTSC)
  157. return ADV7180_INPUT_CONTROL_NTSC_M;
  158. if (std & V4L2_STD_SECAM)
  159. return ADV7180_INPUT_CONTROL_PAL_SECAM;
  160. return -EINVAL;
  161. }
  162. static u32 adv7180_status_to_v4l2(u8 status1)
  163. {
  164. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  165. return V4L2_IN_ST_NO_SIGNAL;
  166. return 0;
  167. }
  168. static int __adv7180_status(struct i2c_client *client, u32 *status,
  169. v4l2_std_id *std)
  170. {
  171. int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
  172. if (status1 < 0)
  173. return status1;
  174. if (status)
  175. *status = adv7180_status_to_v4l2(status1);
  176. if (std)
  177. *std = adv7180_std_to_v4l2(status1);
  178. return 0;
  179. }
  180. static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
  181. {
  182. return container_of(sd, struct adv7180_state, sd);
  183. }
  184. static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  185. {
  186. struct adv7180_state *state = to_state(sd);
  187. int err = mutex_lock_interruptible(&state->mutex);
  188. if (err)
  189. return err;
  190. /* when we are interrupt driven we know the state */
  191. if (!state->autodetect || state->irq > 0)
  192. *std = state->curr_norm;
  193. else
  194. err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
  195. mutex_unlock(&state->mutex);
  196. return err;
  197. }
  198. static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
  199. u32 output, u32 config)
  200. {
  201. struct adv7180_state *state = to_state(sd);
  202. int ret = mutex_lock_interruptible(&state->mutex);
  203. struct i2c_client *client = v4l2_get_subdevdata(sd);
  204. if (ret)
  205. return ret;
  206. /*We cannot discriminate between LQFP and 40-pin LFCSP, so accept
  207. * all inputs and let the card driver take care of validation
  208. */
  209. if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input)
  210. goto out;
  211. ret = i2c_smbus_read_byte_data(client, ADV7180_INPUT_CONTROL_REG);
  212. if (ret < 0)
  213. goto out;
  214. ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
  215. ret = i2c_smbus_write_byte_data(client,
  216. ADV7180_INPUT_CONTROL_REG, ret | input);
  217. state->input = input;
  218. out:
  219. mutex_unlock(&state->mutex);
  220. return ret;
  221. }
  222. static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
  223. {
  224. struct adv7180_state *state = to_state(sd);
  225. int ret = mutex_lock_interruptible(&state->mutex);
  226. if (ret)
  227. return ret;
  228. ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
  229. mutex_unlock(&state->mutex);
  230. return ret;
  231. }
  232. static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
  233. struct v4l2_dbg_chip_ident *chip)
  234. {
  235. struct i2c_client *client = v4l2_get_subdevdata(sd);
  236. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
  237. }
  238. static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  239. {
  240. struct adv7180_state *state = to_state(sd);
  241. struct i2c_client *client = v4l2_get_subdevdata(sd);
  242. int ret = mutex_lock_interruptible(&state->mutex);
  243. if (ret)
  244. return ret;
  245. /* all standards -> autodetect */
  246. if (std == V4L2_STD_ALL) {
  247. ret =
  248. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  249. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
  250. | state->input);
  251. if (ret < 0)
  252. goto out;
  253. __adv7180_status(client, NULL, &state->curr_norm);
  254. state->autodetect = true;
  255. } else {
  256. ret = v4l2_std_to_adv7180(std);
  257. if (ret < 0)
  258. goto out;
  259. ret = i2c_smbus_write_byte_data(client,
  260. ADV7180_INPUT_CONTROL_REG,
  261. ret | state->input);
  262. if (ret < 0)
  263. goto out;
  264. state->curr_norm = std;
  265. state->autodetect = false;
  266. }
  267. ret = 0;
  268. out:
  269. mutex_unlock(&state->mutex);
  270. return ret;
  271. }
  272. static int adv7180_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  273. {
  274. switch (qc->id) {
  275. case V4L2_CID_BRIGHTNESS:
  276. return v4l2_ctrl_query_fill(qc, BRI_REG_MIN, BRI_REG_MAX,
  277. 1, BRI_REG_DEF);
  278. case V4L2_CID_HUE:
  279. return v4l2_ctrl_query_fill(qc, HUE_REG_MIN, HUE_REG_MAX,
  280. 1, HUE_REG_DEF);
  281. case V4L2_CID_CONTRAST:
  282. return v4l2_ctrl_query_fill(qc, CON_REG_MIN, CON_REG_MAX,
  283. 1, CON_REG_DEF);
  284. case V4L2_CID_SATURATION:
  285. return v4l2_ctrl_query_fill(qc, SAT_REG_MIN, SAT_REG_MAX,
  286. 1, SAT_REG_DEF);
  287. default:
  288. break;
  289. }
  290. return -EINVAL;
  291. }
  292. static int adv7180_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  293. {
  294. struct adv7180_state *state = to_state(sd);
  295. int ret = mutex_lock_interruptible(&state->mutex);
  296. if (ret)
  297. return ret;
  298. switch (ctrl->id) {
  299. case V4L2_CID_BRIGHTNESS:
  300. ctrl->value = state->brightness;
  301. break;
  302. case V4L2_CID_HUE:
  303. ctrl->value = state->hue;
  304. break;
  305. case V4L2_CID_CONTRAST:
  306. ctrl->value = state->contrast;
  307. break;
  308. case V4L2_CID_SATURATION:
  309. ctrl->value = state->saturation;
  310. break;
  311. default:
  312. ret = -EINVAL;
  313. }
  314. mutex_unlock(&state->mutex);
  315. return ret;
  316. }
  317. static int adv7180_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  318. {
  319. struct adv7180_state *state = to_state(sd);
  320. struct i2c_client *client = v4l2_get_subdevdata(sd);
  321. int ret = mutex_lock_interruptible(&state->mutex);
  322. if (ret)
  323. return ret;
  324. switch (ctrl->id) {
  325. case V4L2_CID_BRIGHTNESS:
  326. if ((ctrl->value > BRI_REG_MAX)
  327. || (ctrl->value < BRI_REG_MIN)) {
  328. ret = -ERANGE;
  329. break;
  330. }
  331. state->brightness = ctrl->value;
  332. ret = i2c_smbus_write_byte_data(client,
  333. ADV7180_BRI_REG,
  334. state->brightness);
  335. break;
  336. case V4L2_CID_HUE:
  337. if ((ctrl->value > HUE_REG_MAX)
  338. || (ctrl->value < HUE_REG_MIN)) {
  339. ret = -ERANGE;
  340. break;
  341. }
  342. state->hue = ctrl->value;
  343. /*Hue is inverted according to HSL chart */
  344. ret = i2c_smbus_write_byte_data(client,
  345. ADV7180_HUE_REG, -state->hue);
  346. break;
  347. case V4L2_CID_CONTRAST:
  348. if ((ctrl->value > CON_REG_MAX)
  349. || (ctrl->value < CON_REG_MIN)) {
  350. ret = -ERANGE;
  351. break;
  352. }
  353. state->contrast = ctrl->value;
  354. ret = i2c_smbus_write_byte_data(client,
  355. ADV7180_CON_REG,
  356. state->contrast);
  357. break;
  358. case V4L2_CID_SATURATION:
  359. if ((ctrl->value > SAT_REG_MAX)
  360. || (ctrl->value < SAT_REG_MIN)) {
  361. ret = -ERANGE;
  362. break;
  363. }
  364. /*
  365. *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
  366. *Let's not confuse the user, everybody understands saturation
  367. */
  368. state->saturation = ctrl->value;
  369. ret = i2c_smbus_write_byte_data(client,
  370. ADV7180_SD_SAT_CB_REG,
  371. state->saturation);
  372. if (ret < 0)
  373. break;
  374. ret = i2c_smbus_write_byte_data(client,
  375. ADV7180_SD_SAT_CR_REG,
  376. state->saturation);
  377. break;
  378. default:
  379. ret = -EINVAL;
  380. }
  381. mutex_unlock(&state->mutex);
  382. return ret;
  383. }
  384. static const struct v4l2_subdev_video_ops adv7180_video_ops = {
  385. .querystd = adv7180_querystd,
  386. .g_input_status = adv7180_g_input_status,
  387. .s_routing = adv7180_s_routing,
  388. };
  389. static const struct v4l2_subdev_core_ops adv7180_core_ops = {
  390. .g_chip_ident = adv7180_g_chip_ident,
  391. .s_std = adv7180_s_std,
  392. .queryctrl = adv7180_queryctrl,
  393. .g_ctrl = adv7180_g_ctrl,
  394. .s_ctrl = adv7180_s_ctrl,
  395. };
  396. static const struct v4l2_subdev_ops adv7180_ops = {
  397. .core = &adv7180_core_ops,
  398. .video = &adv7180_video_ops,
  399. };
  400. static void adv7180_work(struct work_struct *work)
  401. {
  402. struct adv7180_state *state = container_of(work, struct adv7180_state,
  403. work);
  404. struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
  405. u8 isr3;
  406. mutex_lock(&state->mutex);
  407. i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  408. ADV7180_ADI_CTRL_IRQ_SPACE);
  409. isr3 = i2c_smbus_read_byte_data(client, ADV7180_ISR3_ADI);
  410. /* clear */
  411. i2c_smbus_write_byte_data(client, ADV7180_ICR3_ADI, isr3);
  412. i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG, 0);
  413. if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
  414. __adv7180_status(client, NULL, &state->curr_norm);
  415. mutex_unlock(&state->mutex);
  416. enable_irq(state->irq);
  417. }
  418. static irqreturn_t adv7180_irq(int irq, void *devid)
  419. {
  420. struct adv7180_state *state = devid;
  421. schedule_work(&state->work);
  422. disable_irq_nosync(state->irq);
  423. return IRQ_HANDLED;
  424. }
  425. static int init_device(struct i2c_client *client, struct adv7180_state *state)
  426. {
  427. int ret;
  428. /* Initialize adv7180 */
  429. /* Enable autodetection */
  430. if (state->autodetect) {
  431. ret =
  432. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  433. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
  434. | state->input);
  435. if (ret < 0)
  436. return ret;
  437. ret =
  438. i2c_smbus_write_byte_data(client,
  439. ADV7180_AUTODETECT_ENABLE_REG,
  440. ADV7180_AUTODETECT_DEFAULT);
  441. if (ret < 0)
  442. return ret;
  443. } else {
  444. ret = v4l2_std_to_adv7180(state->curr_norm);
  445. if (ret < 0)
  446. return ret;
  447. ret =
  448. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  449. ret | state->input);
  450. if (ret < 0)
  451. return ret;
  452. }
  453. /* ITU-R BT.656-4 compatible */
  454. ret = i2c_smbus_write_byte_data(client,
  455. ADV7180_EXTENDED_OUTPUT_CONTROL_REG,
  456. ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
  457. if (ret < 0)
  458. return ret;
  459. /* Manually set V bit end position in NTSC mode */
  460. ret = i2c_smbus_write_byte_data(client,
  461. ADV7180_NTSC_V_BIT_END_REG,
  462. ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
  463. if (ret < 0)
  464. return ret;
  465. /* read current norm */
  466. __adv7180_status(client, NULL, &state->curr_norm);
  467. /* register for interrupts */
  468. if (state->irq > 0) {
  469. ret = request_irq(state->irq, adv7180_irq, 0, DRIVER_NAME,
  470. state);
  471. if (ret)
  472. return ret;
  473. ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  474. ADV7180_ADI_CTRL_IRQ_SPACE);
  475. if (ret < 0)
  476. return ret;
  477. /* config the Interrupt pin to be active low */
  478. ret = i2c_smbus_write_byte_data(client, ADV7180_ICONF1_ADI,
  479. ADV7180_ICONF1_ACTIVE_LOW |
  480. ADV7180_ICONF1_PSYNC_ONLY);
  481. if (ret < 0)
  482. return ret;
  483. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
  484. if (ret < 0)
  485. return ret;
  486. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
  487. if (ret < 0)
  488. return ret;
  489. /* enable AD change interrupts interrupts */
  490. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR3_ADI,
  491. ADV7180_IRQ3_AD_CHANGE);
  492. if (ret < 0)
  493. return ret;
  494. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
  495. if (ret < 0)
  496. return ret;
  497. ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  498. 0);
  499. if (ret < 0)
  500. return ret;
  501. }
  502. /*Set default value for controls */
  503. ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG,
  504. state->brightness);
  505. if (ret < 0)
  506. return ret;
  507. ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, state->hue);
  508. if (ret < 0)
  509. return ret;
  510. ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG,
  511. state->contrast);
  512. if (ret < 0)
  513. return ret;
  514. ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
  515. state->saturation);
  516. if (ret < 0)
  517. return ret;
  518. ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
  519. state->saturation);
  520. if (ret < 0)
  521. return ret;
  522. return 0;
  523. }
  524. static __devinit int adv7180_probe(struct i2c_client *client,
  525. const struct i2c_device_id *id)
  526. {
  527. struct adv7180_state *state;
  528. struct v4l2_subdev *sd;
  529. int ret;
  530. /* Check if the adapter supports the needed features */
  531. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  532. return -EIO;
  533. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  534. client->addr, client->adapter->name);
  535. state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
  536. if (state == NULL) {
  537. ret = -ENOMEM;
  538. goto err;
  539. }
  540. state->irq = client->irq;
  541. INIT_WORK(&state->work, adv7180_work);
  542. mutex_init(&state->mutex);
  543. state->autodetect = true;
  544. state->brightness = BRI_REG_DEF;
  545. state->hue = HUE_REG_DEF;
  546. state->contrast = CON_REG_DEF;
  547. state->saturation = SAT_REG_DEF;
  548. state->input = 0;
  549. sd = &state->sd;
  550. v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
  551. ret = init_device(client, state);
  552. if (0 != ret)
  553. goto err_unreg_subdev;
  554. return 0;
  555. err_unreg_subdev:
  556. mutex_destroy(&state->mutex);
  557. v4l2_device_unregister_subdev(sd);
  558. kfree(state);
  559. err:
  560. printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", ret);
  561. return ret;
  562. }
  563. static __devexit int adv7180_remove(struct i2c_client *client)
  564. {
  565. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  566. struct adv7180_state *state = to_state(sd);
  567. if (state->irq > 0) {
  568. free_irq(client->irq, state);
  569. if (cancel_work_sync(&state->work)) {
  570. /*
  571. * Work was pending, therefore we need to enable
  572. * IRQ here to balance the disable_irq() done in the
  573. * interrupt handler.
  574. */
  575. enable_irq(state->irq);
  576. }
  577. }
  578. mutex_destroy(&state->mutex);
  579. v4l2_device_unregister_subdev(sd);
  580. kfree(to_state(sd));
  581. return 0;
  582. }
  583. static const struct i2c_device_id adv7180_id[] = {
  584. {DRIVER_NAME, 0},
  585. {},
  586. };
  587. #ifdef CONFIG_PM
  588. static int adv7180_suspend(struct i2c_client *client, pm_message_t state)
  589. {
  590. int ret;
  591. ret = i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG,
  592. ADV7180_PWR_MAN_OFF);
  593. if (ret < 0)
  594. return ret;
  595. return 0;
  596. }
  597. static int adv7180_resume(struct i2c_client *client)
  598. {
  599. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  600. struct adv7180_state *state = to_state(sd);
  601. int ret;
  602. ret = i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG,
  603. ADV7180_PWR_MAN_ON);
  604. if (ret < 0)
  605. return ret;
  606. ret = init_device(client, state);
  607. if (ret < 0)
  608. return ret;
  609. return 0;
  610. }
  611. #endif
  612. MODULE_DEVICE_TABLE(i2c, adv7180_id);
  613. static struct i2c_driver adv7180_driver = {
  614. .driver = {
  615. .owner = THIS_MODULE,
  616. .name = DRIVER_NAME,
  617. },
  618. .probe = adv7180_probe,
  619. .remove = __devexit_p(adv7180_remove),
  620. #ifdef CONFIG_PM
  621. .suspend = adv7180_suspend,
  622. .resume = adv7180_resume,
  623. #endif
  624. .id_table = adv7180_id,
  625. };
  626. module_i2c_driver(adv7180_driver);
  627. MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
  628. MODULE_AUTHOR("Mocean Laboratories");
  629. MODULE_LICENSE("GPL v2");