ak881x.c 8.6 KB

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