adv7180.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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-ctrls.h>
  29. #include <media/v4l2-chip-ident.h>
  30. #include <linux/mutex.h>
  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. /* Contrast */
  54. #define ADV7180_CON_REG 0x08 /*Unsigned */
  55. #define ADV7180_CON_MIN 0
  56. #define ADV7180_CON_DEF 128
  57. #define ADV7180_CON_MAX 255
  58. /* Brightness*/
  59. #define ADV7180_BRI_REG 0x0a /*Signed */
  60. #define ADV7180_BRI_MIN -128
  61. #define ADV7180_BRI_DEF 0
  62. #define ADV7180_BRI_MAX 127
  63. /* Hue */
  64. #define ADV7180_HUE_REG 0x0b /*Signed, inverted */
  65. #define ADV7180_HUE_MIN -127
  66. #define ADV7180_HUE_DEF 0
  67. #define ADV7180_HUE_MAX 128
  68. #define ADV7180_ADI_CTRL_REG 0x0e
  69. #define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
  70. #define ADV7180_PWR_MAN_REG 0x0f
  71. #define ADV7180_PWR_MAN_ON 0x04
  72. #define ADV7180_PWR_MAN_OFF 0x24
  73. #define ADV7180_PWR_MAN_RES 0x80
  74. #define ADV7180_STATUS1_REG 0x10
  75. #define ADV7180_STATUS1_IN_LOCK 0x01
  76. #define ADV7180_STATUS1_AUTOD_MASK 0x70
  77. #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
  78. #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
  79. #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
  80. #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
  81. #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
  82. #define ADV7180_STATUS1_AUTOD_SECAM 0x50
  83. #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
  84. #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
  85. #define ADV7180_IDENT_REG 0x11
  86. #define ADV7180_ID_7180 0x18
  87. #define ADV7180_ICONF1_ADI 0x40
  88. #define ADV7180_ICONF1_ACTIVE_LOW 0x01
  89. #define ADV7180_ICONF1_PSYNC_ONLY 0x10
  90. #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
  91. /* Saturation */
  92. #define ADV7180_SD_SAT_CB_REG 0xe3 /*Unsigned */
  93. #define ADV7180_SD_SAT_CR_REG 0xe4 /*Unsigned */
  94. #define ADV7180_SAT_MIN 0
  95. #define ADV7180_SAT_DEF 128
  96. #define ADV7180_SAT_MAX 255
  97. #define ADV7180_IRQ1_LOCK 0x01
  98. #define ADV7180_IRQ1_UNLOCK 0x02
  99. #define ADV7180_ISR1_ADI 0x42
  100. #define ADV7180_ICR1_ADI 0x43
  101. #define ADV7180_IMR1_ADI 0x44
  102. #define ADV7180_IMR2_ADI 0x48
  103. #define ADV7180_IRQ3_AD_CHANGE 0x08
  104. #define ADV7180_ISR3_ADI 0x4A
  105. #define ADV7180_ICR3_ADI 0x4B
  106. #define ADV7180_IMR3_ADI 0x4C
  107. #define ADV7180_IMR4_ADI 0x50
  108. #define ADV7180_NTSC_V_BIT_END_REG 0xE6
  109. #define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F
  110. struct adv7180_state {
  111. struct v4l2_ctrl_handler ctrl_hdl;
  112. struct v4l2_subdev sd;
  113. struct work_struct work;
  114. struct mutex mutex; /* mutual excl. when accessing chip */
  115. int irq;
  116. v4l2_std_id curr_norm;
  117. bool autodetect;
  118. u8 input;
  119. };
  120. #define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler, \
  121. struct adv7180_state, \
  122. ctrl_hdl)->sd)
  123. static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
  124. {
  125. switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
  126. case ADV7180_STATUS1_AUTOD_NTSM_M_J:
  127. return V4L2_STD_NTSC;
  128. case ADV7180_STATUS1_AUTOD_NTSC_4_43:
  129. return V4L2_STD_NTSC_443;
  130. case ADV7180_STATUS1_AUTOD_PAL_M:
  131. return V4L2_STD_PAL_M;
  132. case ADV7180_STATUS1_AUTOD_PAL_60:
  133. return V4L2_STD_PAL_60;
  134. case ADV7180_STATUS1_AUTOD_PAL_B_G:
  135. return V4L2_STD_PAL;
  136. case ADV7180_STATUS1_AUTOD_SECAM:
  137. return V4L2_STD_SECAM;
  138. case ADV7180_STATUS1_AUTOD_PAL_COMB:
  139. return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
  140. case ADV7180_STATUS1_AUTOD_SECAM_525:
  141. return V4L2_STD_SECAM;
  142. default:
  143. return V4L2_STD_UNKNOWN;
  144. }
  145. }
  146. static int v4l2_std_to_adv7180(v4l2_std_id std)
  147. {
  148. if (std == V4L2_STD_PAL_60)
  149. return ADV7180_INPUT_CONTROL_PAL60;
  150. if (std == V4L2_STD_NTSC_443)
  151. return ADV7180_INPUT_CONTROL_NTSC_443;
  152. if (std == V4L2_STD_PAL_N)
  153. return ADV7180_INPUT_CONTROL_PAL_N;
  154. if (std == V4L2_STD_PAL_M)
  155. return ADV7180_INPUT_CONTROL_PAL_M;
  156. if (std == V4L2_STD_PAL_Nc)
  157. return ADV7180_INPUT_CONTROL_PAL_COMB_N;
  158. if (std & V4L2_STD_PAL)
  159. return ADV7180_INPUT_CONTROL_PAL_BG;
  160. if (std & V4L2_STD_NTSC)
  161. return ADV7180_INPUT_CONTROL_NTSC_M;
  162. if (std & V4L2_STD_SECAM)
  163. return ADV7180_INPUT_CONTROL_PAL_SECAM;
  164. return -EINVAL;
  165. }
  166. static u32 adv7180_status_to_v4l2(u8 status1)
  167. {
  168. if (!(status1 & ADV7180_STATUS1_IN_LOCK))
  169. return V4L2_IN_ST_NO_SIGNAL;
  170. return 0;
  171. }
  172. static int __adv7180_status(struct i2c_client *client, u32 *status,
  173. v4l2_std_id *std)
  174. {
  175. int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
  176. if (status1 < 0)
  177. return status1;
  178. if (status)
  179. *status = adv7180_status_to_v4l2(status1);
  180. if (std)
  181. *std = adv7180_std_to_v4l2(status1);
  182. return 0;
  183. }
  184. static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
  185. {
  186. return container_of(sd, struct adv7180_state, sd);
  187. }
  188. static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
  189. {
  190. struct adv7180_state *state = to_state(sd);
  191. int err = mutex_lock_interruptible(&state->mutex);
  192. if (err)
  193. return err;
  194. /* when we are interrupt driven we know the state */
  195. if (!state->autodetect || state->irq > 0)
  196. *std = state->curr_norm;
  197. else
  198. err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
  199. mutex_unlock(&state->mutex);
  200. return err;
  201. }
  202. static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
  203. u32 output, u32 config)
  204. {
  205. struct adv7180_state *state = to_state(sd);
  206. int ret = mutex_lock_interruptible(&state->mutex);
  207. struct i2c_client *client = v4l2_get_subdevdata(sd);
  208. if (ret)
  209. return ret;
  210. /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
  211. * all inputs and let the card driver take care of validation
  212. */
  213. if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input)
  214. goto out;
  215. ret = i2c_smbus_read_byte_data(client, ADV7180_INPUT_CONTROL_REG);
  216. if (ret < 0)
  217. goto out;
  218. ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
  219. ret = i2c_smbus_write_byte_data(client,
  220. ADV7180_INPUT_CONTROL_REG, ret | input);
  221. state->input = input;
  222. out:
  223. mutex_unlock(&state->mutex);
  224. return ret;
  225. }
  226. static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
  227. {
  228. struct adv7180_state *state = to_state(sd);
  229. int ret = mutex_lock_interruptible(&state->mutex);
  230. if (ret)
  231. return ret;
  232. ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
  233. mutex_unlock(&state->mutex);
  234. return ret;
  235. }
  236. static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
  237. struct v4l2_dbg_chip_ident *chip)
  238. {
  239. struct i2c_client *client = v4l2_get_subdevdata(sd);
  240. return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
  241. }
  242. static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  243. {
  244. struct adv7180_state *state = to_state(sd);
  245. struct i2c_client *client = v4l2_get_subdevdata(sd);
  246. int ret = mutex_lock_interruptible(&state->mutex);
  247. if (ret)
  248. return ret;
  249. /* all standards -> autodetect */
  250. if (std == V4L2_STD_ALL) {
  251. ret =
  252. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  253. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
  254. | state->input);
  255. if (ret < 0)
  256. goto out;
  257. __adv7180_status(client, NULL, &state->curr_norm);
  258. state->autodetect = true;
  259. } else {
  260. ret = v4l2_std_to_adv7180(std);
  261. if (ret < 0)
  262. goto out;
  263. ret = i2c_smbus_write_byte_data(client,
  264. ADV7180_INPUT_CONTROL_REG,
  265. ret | state->input);
  266. if (ret < 0)
  267. goto out;
  268. state->curr_norm = std;
  269. state->autodetect = false;
  270. }
  271. ret = 0;
  272. out:
  273. mutex_unlock(&state->mutex);
  274. return ret;
  275. }
  276. static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
  277. {
  278. struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
  279. struct adv7180_state *state = to_state(sd);
  280. struct i2c_client *client = v4l2_get_subdevdata(sd);
  281. int ret = mutex_lock_interruptible(&state->mutex);
  282. int val;
  283. if (ret)
  284. return ret;
  285. val = ctrl->val;
  286. switch (ctrl->id) {
  287. case V4L2_CID_BRIGHTNESS:
  288. ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG, val);
  289. break;
  290. case V4L2_CID_HUE:
  291. /*Hue is inverted according to HSL chart */
  292. ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, -val);
  293. break;
  294. case V4L2_CID_CONTRAST:
  295. ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG, val);
  296. break;
  297. case V4L2_CID_SATURATION:
  298. /*
  299. *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
  300. *Let's not confuse the user, everybody understands saturation
  301. */
  302. ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
  303. val);
  304. if (ret < 0)
  305. break;
  306. ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
  307. val);
  308. break;
  309. default:
  310. ret = -EINVAL;
  311. }
  312. mutex_unlock(&state->mutex);
  313. return ret;
  314. }
  315. static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
  316. .s_ctrl = adv7180_s_ctrl,
  317. };
  318. static int adv7180_init_controls(struct adv7180_state *state)
  319. {
  320. v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
  321. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  322. V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
  323. ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
  324. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  325. V4L2_CID_CONTRAST, ADV7180_CON_MIN,
  326. ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
  327. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  328. V4L2_CID_SATURATION, ADV7180_SAT_MIN,
  329. ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
  330. v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
  331. V4L2_CID_HUE, ADV7180_HUE_MIN,
  332. ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
  333. state->sd.ctrl_handler = &state->ctrl_hdl;
  334. if (state->ctrl_hdl.error) {
  335. int err = state->ctrl_hdl.error;
  336. v4l2_ctrl_handler_free(&state->ctrl_hdl);
  337. return err;
  338. }
  339. v4l2_ctrl_handler_setup(&state->ctrl_hdl);
  340. return 0;
  341. }
  342. static void adv7180_exit_controls(struct adv7180_state *state)
  343. {
  344. v4l2_ctrl_handler_free(&state->ctrl_hdl);
  345. }
  346. static const struct v4l2_subdev_video_ops adv7180_video_ops = {
  347. .querystd = adv7180_querystd,
  348. .g_input_status = adv7180_g_input_status,
  349. .s_routing = adv7180_s_routing,
  350. };
  351. static const struct v4l2_subdev_core_ops adv7180_core_ops = {
  352. .g_chip_ident = adv7180_g_chip_ident,
  353. .s_std = adv7180_s_std,
  354. };
  355. static const struct v4l2_subdev_ops adv7180_ops = {
  356. .core = &adv7180_core_ops,
  357. .video = &adv7180_video_ops,
  358. };
  359. static void adv7180_work(struct work_struct *work)
  360. {
  361. struct adv7180_state *state = container_of(work, struct adv7180_state,
  362. work);
  363. struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
  364. u8 isr3;
  365. mutex_lock(&state->mutex);
  366. i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  367. ADV7180_ADI_CTRL_IRQ_SPACE);
  368. isr3 = i2c_smbus_read_byte_data(client, ADV7180_ISR3_ADI);
  369. /* clear */
  370. i2c_smbus_write_byte_data(client, ADV7180_ICR3_ADI, isr3);
  371. i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG, 0);
  372. if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
  373. __adv7180_status(client, NULL, &state->curr_norm);
  374. mutex_unlock(&state->mutex);
  375. enable_irq(state->irq);
  376. }
  377. static irqreturn_t adv7180_irq(int irq, void *devid)
  378. {
  379. struct adv7180_state *state = devid;
  380. schedule_work(&state->work);
  381. disable_irq_nosync(state->irq);
  382. return IRQ_HANDLED;
  383. }
  384. static int init_device(struct i2c_client *client, struct adv7180_state *state)
  385. {
  386. int ret;
  387. /* Initialize adv7180 */
  388. /* Enable autodetection */
  389. if (state->autodetect) {
  390. ret =
  391. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  392. ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
  393. | state->input);
  394. if (ret < 0)
  395. return ret;
  396. ret =
  397. i2c_smbus_write_byte_data(client,
  398. ADV7180_AUTODETECT_ENABLE_REG,
  399. ADV7180_AUTODETECT_DEFAULT);
  400. if (ret < 0)
  401. return ret;
  402. } else {
  403. ret = v4l2_std_to_adv7180(state->curr_norm);
  404. if (ret < 0)
  405. return ret;
  406. ret =
  407. i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
  408. ret | state->input);
  409. if (ret < 0)
  410. return ret;
  411. }
  412. /* ITU-R BT.656-4 compatible */
  413. ret = i2c_smbus_write_byte_data(client,
  414. ADV7180_EXTENDED_OUTPUT_CONTROL_REG,
  415. ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
  416. if (ret < 0)
  417. return ret;
  418. /* Manually set V bit end position in NTSC mode */
  419. ret = i2c_smbus_write_byte_data(client,
  420. ADV7180_NTSC_V_BIT_END_REG,
  421. ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
  422. if (ret < 0)
  423. return ret;
  424. /* read current norm */
  425. __adv7180_status(client, NULL, &state->curr_norm);
  426. /* register for interrupts */
  427. if (state->irq > 0) {
  428. ret = request_irq(state->irq, adv7180_irq, 0, KBUILD_MODNAME,
  429. state);
  430. if (ret)
  431. return ret;
  432. ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  433. ADV7180_ADI_CTRL_IRQ_SPACE);
  434. if (ret < 0)
  435. return ret;
  436. /* config the Interrupt pin to be active low */
  437. ret = i2c_smbus_write_byte_data(client, ADV7180_ICONF1_ADI,
  438. ADV7180_ICONF1_ACTIVE_LOW |
  439. ADV7180_ICONF1_PSYNC_ONLY);
  440. if (ret < 0)
  441. return ret;
  442. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
  443. if (ret < 0)
  444. return ret;
  445. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
  446. if (ret < 0)
  447. return ret;
  448. /* enable AD change interrupts interrupts */
  449. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR3_ADI,
  450. ADV7180_IRQ3_AD_CHANGE);
  451. if (ret < 0)
  452. return ret;
  453. ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
  454. if (ret < 0)
  455. return ret;
  456. ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
  457. 0);
  458. if (ret < 0)
  459. return ret;
  460. }
  461. return 0;
  462. }
  463. static int adv7180_probe(struct i2c_client *client,
  464. const struct i2c_device_id *id)
  465. {
  466. struct adv7180_state *state;
  467. struct v4l2_subdev *sd;
  468. int ret;
  469. /* Check if the adapter supports the needed features */
  470. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  471. return -EIO;
  472. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  473. client->addr, client->adapter->name);
  474. state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
  475. if (state == NULL) {
  476. ret = -ENOMEM;
  477. goto err;
  478. }
  479. state->irq = client->irq;
  480. INIT_WORK(&state->work, adv7180_work);
  481. mutex_init(&state->mutex);
  482. state->autodetect = true;
  483. state->input = 0;
  484. sd = &state->sd;
  485. v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
  486. ret = adv7180_init_controls(state);
  487. if (ret)
  488. goto err_unreg_subdev;
  489. ret = init_device(client, state);
  490. if (ret)
  491. goto err_free_ctrl;
  492. return 0;
  493. err_free_ctrl:
  494. adv7180_exit_controls(state);
  495. err_unreg_subdev:
  496. mutex_destroy(&state->mutex);
  497. v4l2_device_unregister_subdev(sd);
  498. kfree(state);
  499. err:
  500. printk(KERN_ERR KBUILD_MODNAME ": Failed to probe: %d\n", ret);
  501. return ret;
  502. }
  503. static int adv7180_remove(struct i2c_client *client)
  504. {
  505. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  506. struct adv7180_state *state = to_state(sd);
  507. if (state->irq > 0) {
  508. free_irq(client->irq, state);
  509. if (cancel_work_sync(&state->work)) {
  510. /*
  511. * Work was pending, therefore we need to enable
  512. * IRQ here to balance the disable_irq() done in the
  513. * interrupt handler.
  514. */
  515. enable_irq(state->irq);
  516. }
  517. }
  518. mutex_destroy(&state->mutex);
  519. v4l2_device_unregister_subdev(sd);
  520. kfree(to_state(sd));
  521. return 0;
  522. }
  523. static const struct i2c_device_id adv7180_id[] = {
  524. {KBUILD_MODNAME, 0},
  525. {},
  526. };
  527. #ifdef CONFIG_PM
  528. static int adv7180_suspend(struct i2c_client *client, pm_message_t state)
  529. {
  530. int ret;
  531. ret = i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG,
  532. ADV7180_PWR_MAN_OFF);
  533. if (ret < 0)
  534. return ret;
  535. return 0;
  536. }
  537. static int adv7180_resume(struct i2c_client *client)
  538. {
  539. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  540. struct adv7180_state *state = to_state(sd);
  541. int ret;
  542. ret = i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG,
  543. ADV7180_PWR_MAN_ON);
  544. if (ret < 0)
  545. return ret;
  546. ret = init_device(client, state);
  547. if (ret < 0)
  548. return ret;
  549. return 0;
  550. }
  551. #endif
  552. MODULE_DEVICE_TABLE(i2c, adv7180_id);
  553. static struct i2c_driver adv7180_driver = {
  554. .driver = {
  555. .owner = THIS_MODULE,
  556. .name = KBUILD_MODNAME,
  557. },
  558. .probe = adv7180_probe,
  559. .remove = adv7180_remove,
  560. #ifdef CONFIG_PM
  561. .suspend = adv7180_suspend,
  562. .resume = adv7180_resume,
  563. #endif
  564. .id_table = adv7180_id,
  565. };
  566. module_i2c_driver(adv7180_driver);
  567. MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
  568. MODULE_AUTHOR("Mocean Laboratories");
  569. MODULE_LICENSE("GPL v2");