ak881x.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Driver for AK8813 / AK8814 TV-ecoders from Asahi Kasei Microsystems Co., Ltd. (AKM)
  3. *
  4. * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/i2c.h>
  11. #include <linux/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/videodev2.h>
  15. #include <media/ak881x.h>
  16. #include <media/v4l2-chip-ident.h>
  17. #include <media/v4l2-common.h>
  18. #include <media/v4l2-device.h>
  19. #define AK881X_INTERFACE_MODE 0
  20. #define AK881X_VIDEO_PROCESS1 1
  21. #define AK881X_VIDEO_PROCESS2 2
  22. #define AK881X_VIDEO_PROCESS3 3
  23. #define AK881X_DAC_MODE 5
  24. #define AK881X_STATUS 0x24
  25. #define AK881X_DEVICE_ID 0x25
  26. #define AK881X_DEVICE_REVISION 0x26
  27. struct ak881x {
  28. struct v4l2_subdev subdev;
  29. struct ak881x_pdata *pdata;
  30. unsigned int lines;
  31. int id; /* DEVICE_ID code V4L2_IDENT_AK881X code from v4l2-chip-ident.h */
  32. char revision; /* DEVICE_REVISION content */
  33. };
  34. static int reg_read(struct i2c_client *client, const u8 reg)
  35. {
  36. return i2c_smbus_read_byte_data(client, reg);
  37. }
  38. static int reg_write(struct i2c_client *client, const u8 reg,
  39. const u8 data)
  40. {
  41. return i2c_smbus_write_byte_data(client, reg, data);
  42. }
  43. static int reg_set(struct i2c_client *client, const u8 reg,
  44. const u8 data, u8 mask)
  45. {
  46. int ret = reg_read(client, reg);
  47. if (ret < 0)
  48. return ret;
  49. return reg_write(client, reg, (ret & ~mask) | (data & mask));
  50. }
  51. static struct ak881x *to_ak881x(const struct i2c_client *client)
  52. {
  53. return container_of(i2c_get_clientdata(client), struct ak881x, subdev);
  54. }
  55. static int ak881x_g_chip_ident(struct v4l2_subdev *sd,
  56. struct v4l2_dbg_chip_ident *id)
  57. {
  58. struct i2c_client *client = v4l2_get_subdevdata(sd);
  59. struct ak881x *ak881x = to_ak881x(client);
  60. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  61. return -EINVAL;
  62. if (id->match.addr != client->addr)
  63. return -ENODEV;
  64. id->ident = ak881x->id;
  65. id->revision = ak881x->revision;
  66. return 0;
  67. }
  68. #ifdef CONFIG_VIDEO_ADV_DEBUG
  69. static int ak881x_g_register(struct v4l2_subdev *sd,
  70. struct v4l2_dbg_register *reg)
  71. {
  72. struct i2c_client *client = v4l2_get_subdevdata(sd);
  73. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x26)
  74. return -EINVAL;
  75. if (reg->match.addr != client->addr)
  76. return -ENODEV;
  77. reg->val = reg_read(client, reg->reg);
  78. if (reg->val > 0xffff)
  79. return -EIO;
  80. return 0;
  81. }
  82. static int ak881x_s_register(struct v4l2_subdev *sd,
  83. struct v4l2_dbg_register *reg)
  84. {
  85. struct i2c_client *client = v4l2_get_subdevdata(sd);
  86. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0x26)
  87. return -EINVAL;
  88. if (reg->match.addr != client->addr)
  89. return -ENODEV;
  90. if (reg_write(client, reg->reg, reg->val) < 0)
  91. return -EIO;
  92. return 0;
  93. }
  94. #endif
  95. static int ak881x_try_g_mbus_fmt(struct v4l2_subdev *sd,
  96. struct v4l2_mbus_framefmt *mf)
  97. {
  98. struct i2c_client *client = v4l2_get_subdevdata(sd);
  99. struct ak881x *ak881x = to_ak881x(client);
  100. v4l_bound_align_image(&mf->width, 0, 720, 2,
  101. &mf->height, 0, ak881x->lines, 1, 0);
  102. mf->field = V4L2_FIELD_INTERLACED;
  103. mf->code = V4L2_MBUS_FMT_YUYV8_2X8;
  104. mf->colorspace = V4L2_COLORSPACE_SMPTE170M;
  105. return 0;
  106. }
  107. static int ak881x_s_mbus_fmt(struct v4l2_subdev *sd,
  108. struct v4l2_mbus_framefmt *mf)
  109. {
  110. if (mf->field != V4L2_FIELD_INTERLACED ||
  111. mf->code != V4L2_MBUS_FMT_YUYV8_2X8)
  112. return -EINVAL;
  113. return ak881x_try_g_mbus_fmt(sd, mf);
  114. }
  115. static int ak881x_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
  116. enum v4l2_mbus_pixelcode *code)
  117. {
  118. if (index)
  119. return -EINVAL;
  120. *code = V4L2_MBUS_FMT_YUYV8_2X8;
  121. return 0;
  122. }
  123. static int ak881x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  124. {
  125. struct i2c_client *client = v4l2_get_subdevdata(sd);
  126. struct ak881x *ak881x = to_ak881x(client);
  127. a->bounds.left = 0;
  128. a->bounds.top = 0;
  129. a->bounds.width = 720;
  130. a->bounds.height = ak881x->lines;
  131. a->defrect = a->bounds;
  132. a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  133. a->pixelaspect.numerator = 1;
  134. a->pixelaspect.denominator = 1;
  135. return 0;
  136. }
  137. static int ak881x_s_std_output(struct v4l2_subdev *sd, v4l2_std_id std)
  138. {
  139. struct i2c_client *client = v4l2_get_subdevdata(sd);
  140. struct ak881x *ak881x = to_ak881x(client);
  141. u8 vp1;
  142. if (std == V4L2_STD_NTSC_443) {
  143. vp1 = 3;
  144. ak881x->lines = 480;
  145. } else if (std == V4L2_STD_PAL_M) {
  146. vp1 = 5;
  147. ak881x->lines = 480;
  148. } else if (std == V4L2_STD_PAL_60) {
  149. vp1 = 7;
  150. ak881x->lines = 480;
  151. } else if (std && !(std & ~V4L2_STD_PAL)) {
  152. vp1 = 0xf;
  153. ak881x->lines = 576;
  154. } else if (std && !(std & ~V4L2_STD_NTSC)) {
  155. vp1 = 0;
  156. ak881x->lines = 480;
  157. } else {
  158. /* No SECAM or PAL_N/Nc supported */
  159. return -EINVAL;
  160. }
  161. reg_set(client, AK881X_VIDEO_PROCESS1, vp1, 0xf);
  162. return 0;
  163. }
  164. static int ak881x_s_stream(struct v4l2_subdev *sd, int enable)
  165. {
  166. struct i2c_client *client = v4l2_get_subdevdata(sd);
  167. struct ak881x *ak881x = to_ak881x(client);
  168. if (enable) {
  169. u8 dac;
  170. /* For colour-bar testing set bit 6 of AK881X_VIDEO_PROCESS1 */
  171. /* Default: composite output */
  172. if (ak881x->pdata->flags & AK881X_COMPONENT)
  173. dac = 3;
  174. else
  175. dac = 4;
  176. /* Turn on the DAC(s) */
  177. reg_write(client, AK881X_DAC_MODE, dac);
  178. dev_dbg(&client->dev, "chip status 0x%x\n",
  179. reg_read(client, AK881X_STATUS));
  180. } else {
  181. /* ...and clear bit 6 of AK881X_VIDEO_PROCESS1 here */
  182. reg_write(client, AK881X_DAC_MODE, 0);
  183. dev_dbg(&client->dev, "chip status 0x%x\n",
  184. reg_read(client, AK881X_STATUS));
  185. }
  186. return 0;
  187. }
  188. static struct v4l2_subdev_core_ops ak881x_subdev_core_ops = {
  189. .g_chip_ident = ak881x_g_chip_ident,
  190. #ifdef CONFIG_VIDEO_ADV_DEBUG
  191. .g_register = ak881x_g_register,
  192. .s_register = ak881x_s_register,
  193. #endif
  194. };
  195. static struct v4l2_subdev_video_ops ak881x_subdev_video_ops = {
  196. .s_mbus_fmt = ak881x_s_mbus_fmt,
  197. .g_mbus_fmt = ak881x_try_g_mbus_fmt,
  198. .try_mbus_fmt = ak881x_try_g_mbus_fmt,
  199. .cropcap = ak881x_cropcap,
  200. .enum_mbus_fmt = ak881x_enum_mbus_fmt,
  201. .s_std_output = ak881x_s_std_output,
  202. .s_stream = ak881x_s_stream,
  203. };
  204. static struct v4l2_subdev_ops ak881x_subdev_ops = {
  205. .core = &ak881x_subdev_core_ops,
  206. .video = &ak881x_subdev_video_ops,
  207. };
  208. static int ak881x_probe(struct i2c_client *client,
  209. const struct i2c_device_id *did)
  210. {
  211. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  212. struct ak881x *ak881x;
  213. u8 ifmode, data;
  214. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  215. dev_warn(&adapter->dev,
  216. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  217. return -EIO;
  218. }
  219. ak881x = kzalloc(sizeof(struct ak881x), GFP_KERNEL);
  220. if (!ak881x)
  221. return -ENOMEM;
  222. v4l2_i2c_subdev_init(&ak881x->subdev, client, &ak881x_subdev_ops);
  223. data = reg_read(client, AK881X_DEVICE_ID);
  224. switch (data) {
  225. case 0x13:
  226. ak881x->id = V4L2_IDENT_AK8813;
  227. break;
  228. case 0x14:
  229. ak881x->id = V4L2_IDENT_AK8814;
  230. break;
  231. default:
  232. dev_err(&client->dev,
  233. "No ak881x chip detected, register read %x\n", data);
  234. kfree(ak881x);
  235. return -ENODEV;
  236. }
  237. ak881x->revision = reg_read(client, AK881X_DEVICE_REVISION);
  238. ak881x->pdata = client->dev.platform_data;
  239. if (ak881x->pdata) {
  240. if (ak881x->pdata->flags & AK881X_FIELD)
  241. ifmode = 4;
  242. else
  243. ifmode = 0;
  244. switch (ak881x->pdata->flags & AK881X_IF_MODE_MASK) {
  245. case AK881X_IF_MODE_BT656:
  246. ifmode |= 1;
  247. break;
  248. case AK881X_IF_MODE_MASTER:
  249. ifmode |= 2;
  250. break;
  251. case AK881X_IF_MODE_SLAVE:
  252. default:
  253. break;
  254. }
  255. dev_dbg(&client->dev, "IF mode %x\n", ifmode);
  256. /*
  257. * "Line Blanking No." seems to be the same as the number of
  258. * "black" lines on, e.g., SuperH VOU, whose default value of 20
  259. * "incidentally" matches ak881x' default
  260. */
  261. reg_write(client, AK881X_INTERFACE_MODE, ifmode | (20 << 3));
  262. }
  263. /* Hardware default: NTSC-M */
  264. ak881x->lines = 480;
  265. dev_info(&client->dev, "Detected an ak881x chip ID %x, revision %x\n",
  266. data, ak881x->revision);
  267. return 0;
  268. }
  269. static int ak881x_remove(struct i2c_client *client)
  270. {
  271. struct ak881x *ak881x = to_ak881x(client);
  272. v4l2_device_unregister_subdev(&ak881x->subdev);
  273. kfree(ak881x);
  274. return 0;
  275. }
  276. static const struct i2c_device_id ak881x_id[] = {
  277. { "ak8813", 0 },
  278. { "ak8814", 0 },
  279. { }
  280. };
  281. MODULE_DEVICE_TABLE(i2c, ak881x_id);
  282. static struct i2c_driver ak881x_i2c_driver = {
  283. .driver = {
  284. .name = "ak881x",
  285. },
  286. .probe = ak881x_probe,
  287. .remove = ak881x_remove,
  288. .id_table = ak881x_id,
  289. };
  290. static int __init ak881x_module_init(void)
  291. {
  292. return i2c_add_driver(&ak881x_i2c_driver);
  293. }
  294. static void __exit ak881x_module_exit(void)
  295. {
  296. i2c_del_driver(&ak881x_i2c_driver);
  297. }
  298. module_init(ak881x_module_init);
  299. module_exit(ak881x_module_exit);
  300. MODULE_DESCRIPTION("TV-output driver for ak8813/ak8814");
  301. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  302. MODULE_LICENSE("GPL v2");