s2250-board.c 14 KB

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