s2250-board.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright (C) 2008 Sensoray Company Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/usb.h>
  15. #include <linux/i2c.h>
  16. #include <linux/videodev2.h>
  17. #include <linux/slab.h>
  18. #include <media/v4l2-device.h>
  19. #include <media/v4l2-common.h>
  20. #include <media/v4l2-subdev.h>
  21. #include "go7007-priv.h"
  22. MODULE_DESCRIPTION("Sensoray 2250/2251 i2c v4l2 subdev driver");
  23. MODULE_LICENSE("GPL v2");
  24. /*
  25. * Note: this board has two i2c devices: a vpx3226f and a tlv320aic23b.
  26. * Due to the unusual way these are accessed on this device we do not
  27. * reuse the i2c drivers, but instead they are implemented in this
  28. * driver. It would be nice to improve on this, though.
  29. */
  30. #define TLV320_ADDRESS 0x34
  31. #define VPX322_ADDR_ANALOGCONTROL1 0x02
  32. #define VPX322_ADDR_BRIGHTNESS0 0x0127
  33. #define VPX322_ADDR_BRIGHTNESS1 0x0131
  34. #define VPX322_ADDR_CONTRAST0 0x0128
  35. #define VPX322_ADDR_CONTRAST1 0x0132
  36. #define VPX322_ADDR_HUE 0x00dc
  37. #define VPX322_ADDR_SAT 0x0030
  38. struct go7007_usb_board {
  39. unsigned int flags;
  40. struct go7007_board_info main_info;
  41. };
  42. struct go7007_usb {
  43. struct go7007_usb_board *board;
  44. struct mutex i2c_lock;
  45. struct usb_device *usbdev;
  46. struct urb *video_urbs[8];
  47. struct urb *audio_urbs[8];
  48. struct urb *intr_urb;
  49. };
  50. static unsigned char aud_regs[] = {
  51. 0x1e, 0x00,
  52. 0x00, 0x17,
  53. 0x02, 0x17,
  54. 0x04, 0xf9,
  55. 0x06, 0xf9,
  56. 0x08, 0x02,
  57. 0x0a, 0x00,
  58. 0x0c, 0x00,
  59. 0x0a, 0x00,
  60. 0x0c, 0x00,
  61. 0x0e, 0x02,
  62. 0x10, 0x00,
  63. 0x12, 0x01,
  64. 0x00, 0x00,
  65. };
  66. static unsigned char vid_regs[] = {
  67. 0xF2, 0x0f,
  68. 0xAA, 0x00,
  69. 0xF8, 0xff,
  70. 0x00, 0x00,
  71. };
  72. static u16 vid_regs_fp[] = {
  73. 0x028, 0x067,
  74. 0x120, 0x016,
  75. 0x121, 0xcF2,
  76. 0x122, 0x0F2,
  77. 0x123, 0x00c,
  78. 0x124, 0x2d0,
  79. 0x125, 0x2e0,
  80. 0x126, 0x004,
  81. 0x128, 0x1E0,
  82. 0x12A, 0x016,
  83. 0x12B, 0x0F2,
  84. 0x12C, 0x0F2,
  85. 0x12D, 0x00c,
  86. 0x12E, 0x2d0,
  87. 0x12F, 0x2e0,
  88. 0x130, 0x004,
  89. 0x132, 0x1E0,
  90. 0x140, 0x060,
  91. 0x153, 0x00C,
  92. 0x154, 0x200,
  93. 0x150, 0x801,
  94. 0x000, 0x000
  95. };
  96. /* PAL specific values */
  97. static u16 vid_regs_fp_pal[] = {
  98. 0x120, 0x017,
  99. 0x121, 0xd22,
  100. 0x122, 0x122,
  101. 0x12A, 0x017,
  102. 0x12B, 0x122,
  103. 0x12C, 0x122,
  104. 0x140, 0x060,
  105. 0x000, 0x000,
  106. };
  107. struct s2250 {
  108. struct v4l2_subdev sd;
  109. struct v4l2_ctrl_handler hdl;
  110. v4l2_std_id std;
  111. int input;
  112. int brightness;
  113. int contrast;
  114. int saturation;
  115. int hue;
  116. int reg12b_val;
  117. int audio_input;
  118. struct i2c_client *audio;
  119. };
  120. static inline struct s2250 *to_state(struct v4l2_subdev *sd)
  121. {
  122. return container_of(sd, struct s2250, sd);
  123. }
  124. /* from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
  125. static int go7007_usb_vendor_request(struct go7007 *go, u16 request,
  126. u16 value, u16 index, void *transfer_buffer, int length, int in)
  127. {
  128. struct go7007_usb *usb = go->hpi_context;
  129. int timeout = 5000;
  130. if (in) {
  131. return usb_control_msg(usb->usbdev,
  132. usb_rcvctrlpipe(usb->usbdev, 0), request,
  133. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  134. value, index, transfer_buffer, length, timeout);
  135. } else {
  136. return usb_control_msg(usb->usbdev,
  137. usb_sndctrlpipe(usb->usbdev, 0), request,
  138. USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  139. value, index, transfer_buffer, length, timeout);
  140. }
  141. }
  142. /* end from go7007-usb.c which is Copyright (C) 2005-2006 Micronas USA Inc.*/
  143. static int write_reg(struct i2c_client *client, u8 reg, u8 value)
  144. {
  145. struct go7007 *go = i2c_get_adapdata(client->adapter);
  146. struct go7007_usb *usb;
  147. int rc;
  148. int dev_addr = client->addr << 1; /* firmware wants 8-bit address */
  149. u8 *buf;
  150. if (go == NULL)
  151. return -ENODEV;
  152. if (go->status == STATUS_SHUTDOWN)
  153. return -EBUSY;
  154. buf = kzalloc(16, GFP_KERNEL);
  155. if (buf == NULL)
  156. return -ENOMEM;
  157. usb = go->hpi_context;
  158. if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
  159. dev_info(&client->dev, "i2c lock failed\n");
  160. kfree(buf);
  161. return -EINTR;
  162. }
  163. rc = go7007_usb_vendor_request(go, 0x55, dev_addr,
  164. (reg<<8 | value),
  165. buf,
  166. 16, 1);
  167. mutex_unlock(&usb->i2c_lock);
  168. kfree(buf);
  169. return rc;
  170. }
  171. static int write_reg_fp(struct i2c_client *client, u16 addr, u16 val)
  172. {
  173. struct go7007 *go = i2c_get_adapdata(client->adapter);
  174. struct go7007_usb *usb;
  175. int rc;
  176. u8 *buf;
  177. struct s2250 *dec = i2c_get_clientdata(client);
  178. if (go == NULL)
  179. return -ENODEV;
  180. if (go->status == STATUS_SHUTDOWN)
  181. return -EBUSY;
  182. buf = kzalloc(16, GFP_KERNEL);
  183. if (buf == NULL)
  184. return -ENOMEM;
  185. memset(buf, 0xcd, 6);
  186. usb = go->hpi_context;
  187. if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
  188. dev_info(&client->dev, "i2c lock failed\n");
  189. kfree(buf);
  190. return -EINTR;
  191. }
  192. rc = go7007_usb_vendor_request(go, 0x57, addr, val, buf, 16, 1);
  193. mutex_unlock(&usb->i2c_lock);
  194. if (rc < 0) {
  195. kfree(buf);
  196. return rc;
  197. }
  198. if (buf[0] == 0) {
  199. unsigned int subaddr, val_read;
  200. subaddr = (buf[4] << 8) + buf[5];
  201. val_read = (buf[2] << 8) + buf[3];
  202. kfree(buf);
  203. if (val_read != val) {
  204. dev_info(&client->dev, "invalid fp write %x %x\n",
  205. val_read, val);
  206. return -EFAULT;
  207. }
  208. if (subaddr != addr) {
  209. dev_info(&client->dev, "invalid fp write addr %x %x\n",
  210. subaddr, addr);
  211. return -EFAULT;
  212. }
  213. } else {
  214. kfree(buf);
  215. return -EFAULT;
  216. }
  217. /* save last 12b value */
  218. if (addr == 0x12b)
  219. dec->reg12b_val = val;
  220. return 0;
  221. }
  222. static int read_reg_fp(struct i2c_client *client, u16 addr, u16 *val)
  223. {
  224. struct go7007 *go = i2c_get_adapdata(client->adapter);
  225. struct go7007_usb *usb;
  226. int rc;
  227. u8 *buf;
  228. if (go == NULL)
  229. return -ENODEV;
  230. if (go->status == STATUS_SHUTDOWN)
  231. return -EBUSY;
  232. buf = kzalloc(16, GFP_KERNEL);
  233. if (buf == NULL)
  234. return -ENOMEM;
  235. memset(buf, 0xcd, 6);
  236. usb = go->hpi_context;
  237. if (mutex_lock_interruptible(&usb->i2c_lock) != 0) {
  238. dev_info(&client->dev, "i2c lock failed\n");
  239. kfree(buf);
  240. return -EINTR;
  241. }
  242. rc = go7007_usb_vendor_request(go, 0x58, addr, 0, buf, 16, 1);
  243. mutex_unlock(&usb->i2c_lock);
  244. if (rc < 0) {
  245. kfree(buf);
  246. return rc;
  247. }
  248. *val = (buf[0] << 8) | buf[1];
  249. kfree(buf);
  250. return 0;
  251. }
  252. static int write_regs(struct i2c_client *client, u8 *regs)
  253. {
  254. int i;
  255. for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
  256. if (write_reg(client, regs[i], regs[i+1]) < 0) {
  257. dev_info(&client->dev, "failed\n");
  258. return -1;
  259. }
  260. }
  261. return 0;
  262. }
  263. static int write_regs_fp(struct i2c_client *client, u16 *regs)
  264. {
  265. int i;
  266. for (i = 0; !((regs[i] == 0x00) && (regs[i+1] == 0x00)); i += 2) {
  267. if (write_reg_fp(client, regs[i], regs[i+1]) < 0) {
  268. dev_info(&client->dev, "failed fp\n");
  269. return -1;
  270. }
  271. }
  272. return 0;
  273. }
  274. /* ------------------------------------------------------------------------- */
  275. static int s2250_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
  276. u32 config)
  277. {
  278. struct s2250 *state = to_state(sd);
  279. struct i2c_client *client = v4l2_get_subdevdata(sd);
  280. int vidsys;
  281. vidsys = (state->std == V4L2_STD_NTSC) ? 0x01 : 0x00;
  282. if (input == 0) {
  283. /* composite */
  284. write_reg_fp(client, 0x20, 0x020 | vidsys);
  285. write_reg_fp(client, 0x21, 0x662);
  286. write_reg_fp(client, 0x140, 0x060);
  287. } else if (input == 1) {
  288. /* S-Video */
  289. write_reg_fp(client, 0x20, 0x040 | vidsys);
  290. write_reg_fp(client, 0x21, 0x666);
  291. write_reg_fp(client, 0x140, 0x060);
  292. } else {
  293. return -EINVAL;
  294. }
  295. state->input = input;
  296. return 0;
  297. }
  298. static int s2250_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
  299. {
  300. struct s2250 *state = to_state(sd);
  301. struct i2c_client *client = v4l2_get_subdevdata(sd);
  302. u16 vidsource;
  303. vidsource = (state->input == 1) ? 0x040 : 0x020;
  304. if (norm & V4L2_STD_625_50) {
  305. write_regs_fp(client, vid_regs_fp);
  306. write_regs_fp(client, vid_regs_fp_pal);
  307. write_reg_fp(client, 0x20, vidsource);
  308. } else {
  309. write_regs_fp(client, vid_regs_fp);
  310. write_reg_fp(client, 0x20, vidsource | 1);
  311. }
  312. state->std = norm;
  313. return 0;
  314. }
  315. static int s2250_s_ctrl(struct v4l2_ctrl *ctrl)
  316. {
  317. struct s2250 *state = container_of(ctrl->handler, struct s2250, hdl);
  318. struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
  319. u16 oldvalue;
  320. switch (ctrl->id) {
  321. case V4L2_CID_BRIGHTNESS:
  322. read_reg_fp(client, VPX322_ADDR_BRIGHTNESS0, &oldvalue);
  323. write_reg_fp(client, VPX322_ADDR_BRIGHTNESS0,
  324. ctrl->val | (oldvalue & ~0xff));
  325. read_reg_fp(client, VPX322_ADDR_BRIGHTNESS1, &oldvalue);
  326. write_reg_fp(client, VPX322_ADDR_BRIGHTNESS1,
  327. ctrl->val | (oldvalue & ~0xff));
  328. write_reg_fp(client, 0x140, 0x60);
  329. break;
  330. case V4L2_CID_CONTRAST:
  331. read_reg_fp(client, VPX322_ADDR_CONTRAST0, &oldvalue);
  332. write_reg_fp(client, VPX322_ADDR_CONTRAST0,
  333. ctrl->val | (oldvalue & ~0x3f));
  334. read_reg_fp(client, VPX322_ADDR_CONTRAST1, &oldvalue);
  335. write_reg_fp(client, VPX322_ADDR_CONTRAST1,
  336. ctrl->val | (oldvalue & ~0x3f));
  337. write_reg_fp(client, 0x140, 0x60);
  338. break;
  339. case V4L2_CID_SATURATION:
  340. write_reg_fp(client, VPX322_ADDR_SAT, ctrl->val);
  341. break;
  342. case V4L2_CID_HUE:
  343. write_reg_fp(client, VPX322_ADDR_HUE, ctrl->val);
  344. break;
  345. default:
  346. return -EINVAL;
  347. }
  348. return 0;
  349. }
  350. static int s2250_set_fmt(struct v4l2_subdev *sd,
  351. struct v4l2_subdev_pad_config *cfg,
  352. struct v4l2_subdev_format *format)
  353. {
  354. struct v4l2_mbus_framefmt *fmt = &format->format;
  355. struct s2250 *state = to_state(sd);
  356. struct i2c_client *client = v4l2_get_subdevdata(sd);
  357. if (format->pad)
  358. return -EINVAL;
  359. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  360. return 0;
  361. if (fmt->height < 640) {
  362. write_reg_fp(client, 0x12b, state->reg12b_val | 0x400);
  363. write_reg_fp(client, 0x140, 0x060);
  364. } else {
  365. write_reg_fp(client, 0x12b, state->reg12b_val & ~0x400);
  366. write_reg_fp(client, 0x140, 0x060);
  367. }
  368. return 0;
  369. }
  370. static int s2250_s_audio_routing(struct v4l2_subdev *sd, u32 input, u32 output,
  371. u32 config)
  372. {
  373. struct s2250 *state = to_state(sd);
  374. switch (input) {
  375. case 0:
  376. write_reg(state->audio, 0x08, 0x02); /* Line In */
  377. break;
  378. case 1:
  379. write_reg(state->audio, 0x08, 0x04); /* Mic */
  380. break;
  381. case 2:
  382. write_reg(state->audio, 0x08, 0x05); /* Mic Boost */
  383. break;
  384. default:
  385. return -EINVAL;
  386. }
  387. state->audio_input = input;
  388. return 0;
  389. }
  390. static int s2250_log_status(struct v4l2_subdev *sd)
  391. {
  392. struct s2250 *state = to_state(sd);
  393. v4l2_info(sd, "Standard: %s\n", state->std == V4L2_STD_NTSC ? "NTSC" :
  394. state->std == V4L2_STD_PAL ? "PAL" :
  395. state->std == V4L2_STD_SECAM ? "SECAM" :
  396. "unknown");
  397. v4l2_info(sd, "Input: %s\n", state->input == 0 ? "Composite" :
  398. state->input == 1 ? "S-video" :
  399. "error");
  400. v4l2_info(sd, "Audio input: %s\n", state->audio_input == 0 ? "Line In" :
  401. state->audio_input == 1 ? "Mic" :
  402. state->audio_input == 2 ? "Mic Boost" :
  403. "error");
  404. return v4l2_ctrl_subdev_log_status(sd);
  405. }
  406. /* --------------------------------------------------------------------------*/
  407. static const struct v4l2_ctrl_ops s2250_ctrl_ops = {
  408. .s_ctrl = s2250_s_ctrl,
  409. };
  410. static const struct v4l2_subdev_core_ops s2250_core_ops = {
  411. .log_status = s2250_log_status,
  412. };
  413. static const struct v4l2_subdev_audio_ops s2250_audio_ops = {
  414. .s_routing = s2250_s_audio_routing,
  415. };
  416. static const struct v4l2_subdev_video_ops s2250_video_ops = {
  417. .s_std = s2250_s_std,
  418. .s_routing = s2250_s_video_routing,
  419. };
  420. static const struct v4l2_subdev_pad_ops s2250_pad_ops = {
  421. .set_fmt = s2250_set_fmt,
  422. };
  423. static const struct v4l2_subdev_ops s2250_ops = {
  424. .core = &s2250_core_ops,
  425. .audio = &s2250_audio_ops,
  426. .video = &s2250_video_ops,
  427. .pad = &s2250_pad_ops,
  428. };
  429. /* --------------------------------------------------------------------------*/
  430. static int s2250_probe(struct i2c_client *client,
  431. const struct i2c_device_id *id)
  432. {
  433. struct i2c_client *audio;
  434. struct i2c_adapter *adapter = client->adapter;
  435. struct s2250 *state;
  436. struct v4l2_subdev *sd;
  437. u8 *data;
  438. struct go7007 *go = i2c_get_adapdata(adapter);
  439. struct go7007_usb *usb = go->hpi_context;
  440. audio = i2c_new_dummy(adapter, TLV320_ADDRESS >> 1);
  441. if (audio == NULL)
  442. return -ENOMEM;
  443. state = kzalloc(sizeof(struct s2250), GFP_KERNEL);
  444. if (state == NULL) {
  445. i2c_unregister_device(audio);
  446. return -ENOMEM;
  447. }
  448. sd = &state->sd;
  449. v4l2_i2c_subdev_init(sd, client, &s2250_ops);
  450. v4l2_info(sd, "initializing %s at address 0x%x on %s\n",
  451. "Sensoray 2250/2251", client->addr, client->adapter->name);
  452. v4l2_ctrl_handler_init(&state->hdl, 4);
  453. v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
  454. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  455. v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
  456. V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x32);
  457. v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
  458. V4L2_CID_SATURATION, 0, 4094, 1, 2070);
  459. v4l2_ctrl_new_std(&state->hdl, &s2250_ctrl_ops,
  460. V4L2_CID_HUE, -512, 511, 1, 0);
  461. sd->ctrl_handler = &state->hdl;
  462. if (state->hdl.error) {
  463. int err = state->hdl.error;
  464. v4l2_ctrl_handler_free(&state->hdl);
  465. kfree(state);
  466. return err;
  467. }
  468. state->std = V4L2_STD_NTSC;
  469. state->brightness = 50;
  470. state->contrast = 50;
  471. state->saturation = 50;
  472. state->hue = 0;
  473. state->audio = audio;
  474. /* initialize the audio */
  475. if (write_regs(audio, aud_regs) < 0) {
  476. dev_err(&client->dev, "error initializing audio\n");
  477. goto fail;
  478. }
  479. if (write_regs(client, vid_regs) < 0) {
  480. dev_err(&client->dev, "error initializing decoder\n");
  481. goto fail;
  482. }
  483. if (write_regs_fp(client, vid_regs_fp) < 0) {
  484. dev_err(&client->dev, "error initializing decoder\n");
  485. goto fail;
  486. }
  487. /* set default channel */
  488. /* composite */
  489. write_reg_fp(client, 0x20, 0x020 | 1);
  490. write_reg_fp(client, 0x21, 0x662);
  491. write_reg_fp(client, 0x140, 0x060);
  492. /* set default audio input */
  493. state->audio_input = 0;
  494. write_reg(client, 0x08, 0x02); /* Line In */
  495. if (mutex_lock_interruptible(&usb->i2c_lock) == 0) {
  496. data = kzalloc(16, GFP_KERNEL);
  497. if (data != NULL) {
  498. int rc = go7007_usb_vendor_request(go, 0x41, 0, 0,
  499. data, 16, 1);
  500. if (rc > 0) {
  501. u8 mask;
  502. data[0] = 0;
  503. mask = 1<<5;
  504. data[0] &= ~mask;
  505. data[1] |= mask;
  506. go7007_usb_vendor_request(go, 0x40, 0,
  507. (data[1]<<8)
  508. + data[1],
  509. data, 16, 0);
  510. }
  511. kfree(data);
  512. }
  513. mutex_unlock(&usb->i2c_lock);
  514. }
  515. v4l2_info(sd, "initialized successfully\n");
  516. return 0;
  517. fail:
  518. i2c_unregister_device(audio);
  519. v4l2_ctrl_handler_free(&state->hdl);
  520. kfree(state);
  521. return -EIO;
  522. }
  523. static int s2250_remove(struct i2c_client *client)
  524. {
  525. struct s2250 *state = to_state(i2c_get_clientdata(client));
  526. v4l2_device_unregister_subdev(&state->sd);
  527. v4l2_ctrl_handler_free(&state->hdl);
  528. kfree(state);
  529. return 0;
  530. }
  531. static const struct i2c_device_id s2250_id[] = {
  532. { "s2250", 0 },
  533. { }
  534. };
  535. MODULE_DEVICE_TABLE(i2c, s2250_id);
  536. static struct i2c_driver s2250_driver = {
  537. .driver = {
  538. .name = "s2250",
  539. },
  540. .probe = s2250_probe,
  541. .remove = s2250_remove,
  542. .id_table = s2250_id,
  543. };
  544. module_i2c_driver(s2250_driver);