vpbe.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Copyright (C) 2010 Texas Instruments 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 as published by
  6. * the Free Software Foundation version 2.
  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/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/fs.h>
  18. #include <linux/string.h>
  19. #include <linux/wait.h>
  20. #include <linux/time.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <media/v4l2-device.h>
  27. #include <media/davinci/vpbe_types.h>
  28. #include <media/davinci/vpbe.h>
  29. #include <media/davinci/vpss.h>
  30. #include <media/davinci/vpbe_venc.h>
  31. #define VPBE_DEFAULT_OUTPUT "Composite"
  32. #define VPBE_DEFAULT_MODE "ntsc"
  33. static char *def_output = VPBE_DEFAULT_OUTPUT;
  34. static char *def_mode = VPBE_DEFAULT_MODE;
  35. static int debug;
  36. module_param(def_output, charp, S_IRUGO);
  37. module_param(def_mode, charp, S_IRUGO);
  38. module_param(debug, int, 0644);
  39. MODULE_PARM_DESC(def_output, "vpbe output name (default:Composite)");
  40. MODULE_PARM_DESC(def_mode, "vpbe output mode name (default:ntsc");
  41. MODULE_PARM_DESC(debug, "Debug level 0-1");
  42. MODULE_DESCRIPTION("TI DMXXX VPBE Display controller");
  43. MODULE_LICENSE("GPL");
  44. MODULE_AUTHOR("Texas Instruments");
  45. /**
  46. * vpbe_current_encoder_info - Get config info for current encoder
  47. * @vpbe_dev - vpbe device ptr
  48. *
  49. * Return ptr to current encoder config info
  50. */
  51. static struct encoder_config_info*
  52. vpbe_current_encoder_info(struct vpbe_device *vpbe_dev)
  53. {
  54. struct vpbe_config *cfg = vpbe_dev->cfg;
  55. int index = vpbe_dev->current_sd_index;
  56. return ((index == 0) ? &cfg->venc :
  57. &cfg->ext_encoders[index-1]);
  58. }
  59. /**
  60. * vpbe_find_encoder_sd_index - Given a name find encoder sd index
  61. *
  62. * @vpbe_config - ptr to vpbe cfg
  63. * @output_index - index used by application
  64. *
  65. * Return sd index of the encoder
  66. */
  67. static int vpbe_find_encoder_sd_index(struct vpbe_config *cfg,
  68. int index)
  69. {
  70. char *encoder_name = cfg->outputs[index].subdev_name;
  71. int i;
  72. /* Venc is always first */
  73. if (!strcmp(encoder_name, cfg->venc.module_name))
  74. return 0;
  75. for (i = 0; i < cfg->num_ext_encoders; i++) {
  76. if (!strcmp(encoder_name,
  77. cfg->ext_encoders[i].module_name))
  78. return i+1;
  79. }
  80. return -EINVAL;
  81. }
  82. /**
  83. * vpbe_g_cropcap - Get crop capabilities of the display
  84. * @vpbe_dev - vpbe device ptr
  85. * @cropcap - cropcap is a ptr to struct v4l2_cropcap
  86. *
  87. * Update the crop capabilities in crop cap for current
  88. * mode
  89. */
  90. static int vpbe_g_cropcap(struct vpbe_device *vpbe_dev,
  91. struct v4l2_cropcap *cropcap)
  92. {
  93. if (!cropcap)
  94. return -EINVAL;
  95. cropcap->bounds.left = 0;
  96. cropcap->bounds.top = 0;
  97. cropcap->bounds.width = vpbe_dev->current_timings.xres;
  98. cropcap->bounds.height = vpbe_dev->current_timings.yres;
  99. cropcap->defrect = cropcap->bounds;
  100. return 0;
  101. }
  102. /**
  103. * vpbe_enum_outputs - enumerate outputs
  104. * @vpbe_dev - vpbe device ptr
  105. * @output - ptr to v4l2_output structure
  106. *
  107. * Enumerates the outputs available at the vpbe display
  108. * returns the status, -EINVAL if end of output list
  109. */
  110. static int vpbe_enum_outputs(struct vpbe_device *vpbe_dev,
  111. struct v4l2_output *output)
  112. {
  113. struct vpbe_config *cfg = vpbe_dev->cfg;
  114. int temp_index = output->index;
  115. if (temp_index >= cfg->num_outputs)
  116. return -EINVAL;
  117. *output = cfg->outputs[temp_index].output;
  118. output->index = temp_index;
  119. return 0;
  120. }
  121. static int vpbe_get_mode_info(struct vpbe_device *vpbe_dev, char *mode,
  122. int output_index)
  123. {
  124. struct vpbe_config *cfg = vpbe_dev->cfg;
  125. struct vpbe_enc_mode_info var;
  126. int curr_output = output_index;
  127. int i;
  128. if (!mode)
  129. return -EINVAL;
  130. for (i = 0; i < cfg->outputs[curr_output].num_modes; i++) {
  131. var = cfg->outputs[curr_output].modes[i];
  132. if (!strcmp(mode, var.name)) {
  133. vpbe_dev->current_timings = var;
  134. return 0;
  135. }
  136. }
  137. return -EINVAL;
  138. }
  139. static int vpbe_get_current_mode_info(struct vpbe_device *vpbe_dev,
  140. struct vpbe_enc_mode_info *mode_info)
  141. {
  142. if (!mode_info)
  143. return -EINVAL;
  144. *mode_info = vpbe_dev->current_timings;
  145. return 0;
  146. }
  147. /* Get std by std id */
  148. static int vpbe_get_std_info(struct vpbe_device *vpbe_dev,
  149. v4l2_std_id std_id)
  150. {
  151. struct vpbe_config *cfg = vpbe_dev->cfg;
  152. struct vpbe_enc_mode_info var;
  153. int curr_output = vpbe_dev->current_out_index;
  154. int i;
  155. for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
  156. var = cfg->outputs[curr_output].modes[i];
  157. if ((var.timings_type & VPBE_ENC_STD) &&
  158. (var.std_id & std_id)) {
  159. vpbe_dev->current_timings = var;
  160. return 0;
  161. }
  162. }
  163. return -EINVAL;
  164. }
  165. static int vpbe_get_std_info_by_name(struct vpbe_device *vpbe_dev,
  166. char *std_name)
  167. {
  168. struct vpbe_config *cfg = vpbe_dev->cfg;
  169. struct vpbe_enc_mode_info var;
  170. int curr_output = vpbe_dev->current_out_index;
  171. int i;
  172. for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
  173. var = cfg->outputs[curr_output].modes[i];
  174. if (!strcmp(var.name, std_name)) {
  175. vpbe_dev->current_timings = var;
  176. return 0;
  177. }
  178. }
  179. return -EINVAL;
  180. }
  181. /**
  182. * vpbe_set_output - Set output
  183. * @vpbe_dev - vpbe device ptr
  184. * @index - index of output
  185. *
  186. * Set vpbe output to the output specified by the index
  187. */
  188. static int vpbe_set_output(struct vpbe_device *vpbe_dev, int index)
  189. {
  190. struct encoder_config_info *curr_enc_info =
  191. vpbe_current_encoder_info(vpbe_dev);
  192. struct vpbe_config *cfg = vpbe_dev->cfg;
  193. struct venc_platform_data *venc_device = vpbe_dev->venc_device;
  194. int enc_out_index;
  195. int sd_index;
  196. int ret;
  197. if (index >= cfg->num_outputs)
  198. return -EINVAL;
  199. mutex_lock(&vpbe_dev->lock);
  200. sd_index = vpbe_dev->current_sd_index;
  201. enc_out_index = cfg->outputs[index].output.index;
  202. /*
  203. * Currently we switch the encoder based on output selected
  204. * by the application. If media controller is implemented later
  205. * there is will be an API added to setup_link between venc
  206. * and external encoder. So in that case below comparison always
  207. * match and encoder will not be switched. But if application
  208. * chose not to use media controller, then this provides current
  209. * way of switching encoder at the venc output.
  210. */
  211. if (strcmp(curr_enc_info->module_name,
  212. cfg->outputs[index].subdev_name)) {
  213. /* Need to switch the encoder at the output */
  214. sd_index = vpbe_find_encoder_sd_index(cfg, index);
  215. if (sd_index < 0) {
  216. ret = -EINVAL;
  217. goto unlock;
  218. }
  219. ret = venc_device->setup_if_config(cfg->outputs[index].if_params);
  220. if (ret)
  221. goto unlock;
  222. }
  223. /* Set output at the encoder */
  224. ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
  225. s_routing, 0, enc_out_index, 0);
  226. if (ret)
  227. goto unlock;
  228. /*
  229. * It is assumed that venc or extenal encoder will set a default
  230. * mode in the sub device. For external encoder or LCD pannel output,
  231. * we also need to set up the lcd port for the required mode. So setup
  232. * the lcd port for the default mode that is configured in the board
  233. * arch/arm/mach-davinci/board-dm355-evm.setup file for the external
  234. * encoder.
  235. */
  236. ret = vpbe_get_mode_info(vpbe_dev,
  237. cfg->outputs[index].default_mode, index);
  238. if (!ret) {
  239. struct osd_state *osd_device = vpbe_dev->osd_device;
  240. osd_device->ops.set_left_margin(osd_device,
  241. vpbe_dev->current_timings.left_margin);
  242. osd_device->ops.set_top_margin(osd_device,
  243. vpbe_dev->current_timings.upper_margin);
  244. vpbe_dev->current_sd_index = sd_index;
  245. vpbe_dev->current_out_index = index;
  246. }
  247. unlock:
  248. mutex_unlock(&vpbe_dev->lock);
  249. return ret;
  250. }
  251. static int vpbe_set_default_output(struct vpbe_device *vpbe_dev)
  252. {
  253. struct vpbe_config *cfg = vpbe_dev->cfg;
  254. int i;
  255. for (i = 0; i < cfg->num_outputs; i++) {
  256. if (!strcmp(def_output,
  257. cfg->outputs[i].output.name)) {
  258. int ret = vpbe_set_output(vpbe_dev, i);
  259. if (!ret)
  260. vpbe_dev->current_out_index = i;
  261. return ret;
  262. }
  263. }
  264. return 0;
  265. }
  266. /**
  267. * vpbe_get_output - Get output
  268. * @vpbe_dev - vpbe device ptr
  269. *
  270. * return current vpbe output to the the index
  271. */
  272. static unsigned int vpbe_get_output(struct vpbe_device *vpbe_dev)
  273. {
  274. return vpbe_dev->current_out_index;
  275. }
  276. /**
  277. * vpbe_s_dv_timings - Set the given preset timings in the encoder
  278. *
  279. * Sets the timings if supported by the current encoder. Return the status.
  280. * 0 - success & -EINVAL on error
  281. */
  282. static int vpbe_s_dv_timings(struct vpbe_device *vpbe_dev,
  283. struct v4l2_dv_timings *dv_timings)
  284. {
  285. struct vpbe_config *cfg = vpbe_dev->cfg;
  286. int out_index = vpbe_dev->current_out_index;
  287. struct vpbe_output *output = &cfg->outputs[out_index];
  288. int sd_index = vpbe_dev->current_sd_index;
  289. int ret, i;
  290. if (!(cfg->outputs[out_index].output.capabilities &
  291. V4L2_OUT_CAP_DV_TIMINGS))
  292. return -ENODATA;
  293. for (i = 0; i < output->num_modes; i++) {
  294. if (output->modes[i].timings_type == VPBE_ENC_DV_TIMINGS &&
  295. !memcmp(&output->modes[i].dv_timings,
  296. dv_timings, sizeof(*dv_timings)))
  297. break;
  298. }
  299. if (i >= output->num_modes)
  300. return -EINVAL;
  301. vpbe_dev->current_timings = output->modes[i];
  302. mutex_lock(&vpbe_dev->lock);
  303. ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
  304. s_dv_timings, dv_timings);
  305. if (!ret && vpbe_dev->amp) {
  306. /* Call amplifier subdevice */
  307. ret = v4l2_subdev_call(vpbe_dev->amp, video,
  308. s_dv_timings, dv_timings);
  309. }
  310. /* set the lcd controller output for the given mode */
  311. if (!ret) {
  312. struct osd_state *osd_device = vpbe_dev->osd_device;
  313. osd_device->ops.set_left_margin(osd_device,
  314. vpbe_dev->current_timings.left_margin);
  315. osd_device->ops.set_top_margin(osd_device,
  316. vpbe_dev->current_timings.upper_margin);
  317. }
  318. mutex_unlock(&vpbe_dev->lock);
  319. return ret;
  320. }
  321. /**
  322. * vpbe_g_dv_timings - Get the timings in the current encoder
  323. *
  324. * Get the timings in the current encoder. Return the status. 0 - success
  325. * -EINVAL on error
  326. */
  327. static int vpbe_g_dv_timings(struct vpbe_device *vpbe_dev,
  328. struct v4l2_dv_timings *dv_timings)
  329. {
  330. struct vpbe_config *cfg = vpbe_dev->cfg;
  331. int out_index = vpbe_dev->current_out_index;
  332. if (!(cfg->outputs[out_index].output.capabilities &
  333. V4L2_OUT_CAP_DV_TIMINGS))
  334. return -ENODATA;
  335. if (vpbe_dev->current_timings.timings_type &
  336. VPBE_ENC_DV_TIMINGS) {
  337. *dv_timings = vpbe_dev->current_timings.dv_timings;
  338. return 0;
  339. }
  340. return -EINVAL;
  341. }
  342. /**
  343. * vpbe_enum_dv_timings - Enumerate the dv timings in the current encoder
  344. *
  345. * Get the timings in the current encoder. Return the status. 0 - success
  346. * -EINVAL on error
  347. */
  348. static int vpbe_enum_dv_timings(struct vpbe_device *vpbe_dev,
  349. struct v4l2_enum_dv_timings *timings)
  350. {
  351. struct vpbe_config *cfg = vpbe_dev->cfg;
  352. int out_index = vpbe_dev->current_out_index;
  353. struct vpbe_output *output = &cfg->outputs[out_index];
  354. int j = 0;
  355. int i;
  356. if (!(output->output.capabilities & V4L2_OUT_CAP_DV_TIMINGS))
  357. return -ENODATA;
  358. for (i = 0; i < output->num_modes; i++) {
  359. if (output->modes[i].timings_type == VPBE_ENC_DV_TIMINGS) {
  360. if (j == timings->index)
  361. break;
  362. j++;
  363. }
  364. }
  365. if (i == output->num_modes)
  366. return -EINVAL;
  367. timings->timings = output->modes[i].dv_timings;
  368. return 0;
  369. }
  370. /**
  371. * vpbe_s_std - Set the given standard in the encoder
  372. *
  373. * Sets the standard if supported by the current encoder. Return the status.
  374. * 0 - success & -EINVAL on error
  375. */
  376. static int vpbe_s_std(struct vpbe_device *vpbe_dev, v4l2_std_id std_id)
  377. {
  378. struct vpbe_config *cfg = vpbe_dev->cfg;
  379. int out_index = vpbe_dev->current_out_index;
  380. int sd_index = vpbe_dev->current_sd_index;
  381. int ret;
  382. if (!(cfg->outputs[out_index].output.capabilities &
  383. V4L2_OUT_CAP_STD))
  384. return -ENODATA;
  385. ret = vpbe_get_std_info(vpbe_dev, std_id);
  386. if (ret)
  387. return ret;
  388. mutex_lock(&vpbe_dev->lock);
  389. ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
  390. s_std_output, std_id);
  391. /* set the lcd controller output for the given mode */
  392. if (!ret) {
  393. struct osd_state *osd_device = vpbe_dev->osd_device;
  394. osd_device->ops.set_left_margin(osd_device,
  395. vpbe_dev->current_timings.left_margin);
  396. osd_device->ops.set_top_margin(osd_device,
  397. vpbe_dev->current_timings.upper_margin);
  398. }
  399. mutex_unlock(&vpbe_dev->lock);
  400. return ret;
  401. }
  402. /**
  403. * vpbe_g_std - Get the standard in the current encoder
  404. *
  405. * Get the standard in the current encoder. Return the status. 0 - success
  406. * -EINVAL on error
  407. */
  408. static int vpbe_g_std(struct vpbe_device *vpbe_dev, v4l2_std_id *std_id)
  409. {
  410. struct vpbe_enc_mode_info *cur_timings = &vpbe_dev->current_timings;
  411. struct vpbe_config *cfg = vpbe_dev->cfg;
  412. int out_index = vpbe_dev->current_out_index;
  413. if (!(cfg->outputs[out_index].output.capabilities & V4L2_OUT_CAP_STD))
  414. return -ENODATA;
  415. if (cur_timings->timings_type & VPBE_ENC_STD) {
  416. *std_id = cur_timings->std_id;
  417. return 0;
  418. }
  419. return -EINVAL;
  420. }
  421. /**
  422. * vpbe_set_mode - Set mode in the current encoder using mode info
  423. *
  424. * Use the mode string to decide what timings to set in the encoder
  425. * This is typically useful when fbset command is used to change the current
  426. * timings by specifying a string to indicate the timings.
  427. */
  428. static int vpbe_set_mode(struct vpbe_device *vpbe_dev,
  429. struct vpbe_enc_mode_info *mode_info)
  430. {
  431. struct vpbe_enc_mode_info *preset_mode = NULL;
  432. struct vpbe_config *cfg = vpbe_dev->cfg;
  433. struct v4l2_dv_timings dv_timings;
  434. struct osd_state *osd_device;
  435. int out_index = vpbe_dev->current_out_index;
  436. int i;
  437. if (!mode_info || !mode_info->name)
  438. return -EINVAL;
  439. for (i = 0; i < cfg->outputs[out_index].num_modes; i++) {
  440. if (!strcmp(mode_info->name,
  441. cfg->outputs[out_index].modes[i].name)) {
  442. preset_mode = &cfg->outputs[out_index].modes[i];
  443. /*
  444. * it may be one of the 3 timings type. Check and
  445. * invoke right API
  446. */
  447. if (preset_mode->timings_type & VPBE_ENC_STD)
  448. return vpbe_s_std(vpbe_dev,
  449. preset_mode->std_id);
  450. if (preset_mode->timings_type &
  451. VPBE_ENC_DV_TIMINGS) {
  452. dv_timings =
  453. preset_mode->dv_timings;
  454. return vpbe_s_dv_timings(vpbe_dev, &dv_timings);
  455. }
  456. }
  457. }
  458. /* Only custom timing should reach here */
  459. if (!preset_mode)
  460. return -EINVAL;
  461. mutex_lock(&vpbe_dev->lock);
  462. osd_device = vpbe_dev->osd_device;
  463. vpbe_dev->current_timings = *preset_mode;
  464. osd_device->ops.set_left_margin(osd_device,
  465. vpbe_dev->current_timings.left_margin);
  466. osd_device->ops.set_top_margin(osd_device,
  467. vpbe_dev->current_timings.upper_margin);
  468. mutex_unlock(&vpbe_dev->lock);
  469. return 0;
  470. }
  471. static int vpbe_set_default_mode(struct vpbe_device *vpbe_dev)
  472. {
  473. int ret;
  474. ret = vpbe_get_std_info_by_name(vpbe_dev, def_mode);
  475. if (ret)
  476. return ret;
  477. /* set the default mode in the encoder */
  478. return vpbe_set_mode(vpbe_dev, &vpbe_dev->current_timings);
  479. }
  480. static int platform_device_get(struct device *dev, void *data)
  481. {
  482. struct platform_device *pdev = to_platform_device(dev);
  483. struct vpbe_device *vpbe_dev = data;
  484. if (strstr(pdev->name, "vpbe-osd"))
  485. vpbe_dev->osd_device = platform_get_drvdata(pdev);
  486. if (strstr(pdev->name, "vpbe-venc"))
  487. vpbe_dev->venc_device = dev_get_platdata(&pdev->dev);
  488. return 0;
  489. }
  490. /**
  491. * vpbe_initialize() - Initialize the vpbe display controller
  492. * @vpbe_dev - vpbe device ptr
  493. *
  494. * Master frame buffer device drivers calls this to initialize vpbe
  495. * display controller. This will then registers v4l2 device and the sub
  496. * devices and sets a current encoder sub device for display. v4l2 display
  497. * device driver is the master and frame buffer display device driver is
  498. * the slave. Frame buffer display driver checks the initialized during
  499. * probe and exit if not initialized. Returns status.
  500. */
  501. static int vpbe_initialize(struct device *dev, struct vpbe_device *vpbe_dev)
  502. {
  503. struct encoder_config_info *enc_info;
  504. struct amp_config_info *amp_info;
  505. struct v4l2_subdev **enc_subdev;
  506. struct osd_state *osd_device;
  507. struct i2c_adapter *i2c_adap;
  508. int num_encoders;
  509. int ret = 0;
  510. int err;
  511. int i;
  512. /*
  513. * v4l2 abd FBDev frame buffer devices will get the vpbe_dev pointer
  514. * from the platform device by iteration of platform drivers and
  515. * matching with device name
  516. */
  517. if (!vpbe_dev || !dev) {
  518. printk(KERN_ERR "Null device pointers.\n");
  519. return -ENODEV;
  520. }
  521. if (vpbe_dev->initialized)
  522. return 0;
  523. mutex_lock(&vpbe_dev->lock);
  524. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  525. /* We have dac clock available for platform */
  526. vpbe_dev->dac_clk = clk_get(vpbe_dev->pdev, "vpss_dac");
  527. if (IS_ERR(vpbe_dev->dac_clk)) {
  528. ret = PTR_ERR(vpbe_dev->dac_clk);
  529. goto fail_mutex_unlock;
  530. }
  531. if (clk_prepare_enable(vpbe_dev->dac_clk)) {
  532. ret = -ENODEV;
  533. clk_put(vpbe_dev->dac_clk);
  534. goto fail_mutex_unlock;
  535. }
  536. }
  537. /* first enable vpss clocks */
  538. vpss_enable_clock(VPSS_VPBE_CLOCK, 1);
  539. /* First register a v4l2 device */
  540. ret = v4l2_device_register(dev, &vpbe_dev->v4l2_dev);
  541. if (ret) {
  542. v4l2_err(dev->driver,
  543. "Unable to register v4l2 device.\n");
  544. goto fail_clk_put;
  545. }
  546. v4l2_info(&vpbe_dev->v4l2_dev, "vpbe v4l2 device registered\n");
  547. err = bus_for_each_dev(&platform_bus_type, NULL, vpbe_dev,
  548. platform_device_get);
  549. if (err < 0) {
  550. ret = err;
  551. goto fail_dev_unregister;
  552. }
  553. vpbe_dev->venc = venc_sub_dev_init(&vpbe_dev->v4l2_dev,
  554. vpbe_dev->cfg->venc.module_name);
  555. /* register venc sub device */
  556. if (!vpbe_dev->venc) {
  557. v4l2_err(&vpbe_dev->v4l2_dev,
  558. "vpbe unable to init venc sub device\n");
  559. ret = -ENODEV;
  560. goto fail_dev_unregister;
  561. }
  562. /* initialize osd device */
  563. osd_device = vpbe_dev->osd_device;
  564. if (osd_device->ops.initialize) {
  565. err = osd_device->ops.initialize(osd_device);
  566. if (err) {
  567. v4l2_err(&vpbe_dev->v4l2_dev,
  568. "unable to initialize the OSD device");
  569. err = -ENOMEM;
  570. goto fail_dev_unregister;
  571. }
  572. }
  573. /*
  574. * Register any external encoders that are configured. At index 0 we
  575. * store venc sd index.
  576. */
  577. num_encoders = vpbe_dev->cfg->num_ext_encoders + 1;
  578. vpbe_dev->encoders = kmalloc_array(num_encoders,
  579. sizeof(*vpbe_dev->encoders),
  580. GFP_KERNEL);
  581. if (!vpbe_dev->encoders) {
  582. ret = -ENOMEM;
  583. goto fail_dev_unregister;
  584. }
  585. i2c_adap = i2c_get_adapter(vpbe_dev->cfg->i2c_adapter_id);
  586. for (i = 0; i < (vpbe_dev->cfg->num_ext_encoders + 1); i++) {
  587. if (i == 0) {
  588. /* venc is at index 0 */
  589. enc_subdev = &vpbe_dev->encoders[i];
  590. *enc_subdev = vpbe_dev->venc;
  591. continue;
  592. }
  593. enc_info = &vpbe_dev->cfg->ext_encoders[i];
  594. if (enc_info->is_i2c) {
  595. enc_subdev = &vpbe_dev->encoders[i];
  596. *enc_subdev = v4l2_i2c_new_subdev_board(
  597. &vpbe_dev->v4l2_dev, i2c_adap,
  598. &enc_info->board_info, NULL);
  599. if (*enc_subdev)
  600. v4l2_info(&vpbe_dev->v4l2_dev,
  601. "v4l2 sub device %s registered\n",
  602. enc_info->module_name);
  603. else {
  604. v4l2_err(&vpbe_dev->v4l2_dev, "encoder %s failed to register",
  605. enc_info->module_name);
  606. ret = -ENODEV;
  607. goto fail_kfree_encoders;
  608. }
  609. } else
  610. v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c encoders currently not supported");
  611. }
  612. /* Add amplifier subdevice for dm365 */
  613. if ((strcmp(vpbe_dev->cfg->module_name, "dm365-vpbe-display") == 0) &&
  614. vpbe_dev->cfg->amp) {
  615. amp_info = vpbe_dev->cfg->amp;
  616. if (amp_info->is_i2c) {
  617. vpbe_dev->amp = v4l2_i2c_new_subdev_board(
  618. &vpbe_dev->v4l2_dev, i2c_adap,
  619. &amp_info->board_info, NULL);
  620. if (!vpbe_dev->amp) {
  621. v4l2_err(&vpbe_dev->v4l2_dev,
  622. "amplifier %s failed to register",
  623. amp_info->module_name);
  624. ret = -ENODEV;
  625. goto fail_kfree_encoders;
  626. }
  627. v4l2_info(&vpbe_dev->v4l2_dev,
  628. "v4l2 sub device %s registered\n",
  629. amp_info->module_name);
  630. } else {
  631. vpbe_dev->amp = NULL;
  632. v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c amplifiers currently not supported");
  633. }
  634. } else {
  635. vpbe_dev->amp = NULL;
  636. }
  637. /* set the current encoder and output to that of venc by default */
  638. vpbe_dev->current_sd_index = 0;
  639. vpbe_dev->current_out_index = 0;
  640. mutex_unlock(&vpbe_dev->lock);
  641. printk(KERN_NOTICE "Setting default output to %s\n", def_output);
  642. ret = vpbe_set_default_output(vpbe_dev);
  643. if (ret) {
  644. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default output %s",
  645. def_output);
  646. return ret;
  647. }
  648. printk(KERN_NOTICE "Setting default mode to %s\n", def_mode);
  649. ret = vpbe_set_default_mode(vpbe_dev);
  650. if (ret) {
  651. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default mode %s",
  652. def_mode);
  653. return ret;
  654. }
  655. vpbe_dev->initialized = 1;
  656. /* TBD handling of bootargs for default output and mode */
  657. return 0;
  658. fail_kfree_encoders:
  659. kfree(vpbe_dev->encoders);
  660. fail_dev_unregister:
  661. v4l2_device_unregister(&vpbe_dev->v4l2_dev);
  662. fail_clk_put:
  663. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  664. clk_disable_unprepare(vpbe_dev->dac_clk);
  665. clk_put(vpbe_dev->dac_clk);
  666. }
  667. fail_mutex_unlock:
  668. mutex_unlock(&vpbe_dev->lock);
  669. return ret;
  670. }
  671. /**
  672. * vpbe_deinitialize() - de-initialize the vpbe display controller
  673. * @dev - Master and slave device ptr
  674. *
  675. * vpbe_master and slave frame buffer devices calls this to de-initialize
  676. * the display controller. It is called when master and slave device
  677. * driver modules are removed and no longer requires the display controller.
  678. */
  679. static void vpbe_deinitialize(struct device *dev, struct vpbe_device *vpbe_dev)
  680. {
  681. v4l2_device_unregister(&vpbe_dev->v4l2_dev);
  682. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  683. clk_disable_unprepare(vpbe_dev->dac_clk);
  684. clk_put(vpbe_dev->dac_clk);
  685. }
  686. kfree(vpbe_dev->amp);
  687. kfree(vpbe_dev->encoders);
  688. vpbe_dev->initialized = 0;
  689. /* disable vpss clocks */
  690. vpss_enable_clock(VPSS_VPBE_CLOCK, 0);
  691. }
  692. static struct vpbe_device_ops vpbe_dev_ops = {
  693. .g_cropcap = vpbe_g_cropcap,
  694. .enum_outputs = vpbe_enum_outputs,
  695. .set_output = vpbe_set_output,
  696. .get_output = vpbe_get_output,
  697. .s_dv_timings = vpbe_s_dv_timings,
  698. .g_dv_timings = vpbe_g_dv_timings,
  699. .enum_dv_timings = vpbe_enum_dv_timings,
  700. .s_std = vpbe_s_std,
  701. .g_std = vpbe_g_std,
  702. .initialize = vpbe_initialize,
  703. .deinitialize = vpbe_deinitialize,
  704. .get_mode_info = vpbe_get_current_mode_info,
  705. .set_mode = vpbe_set_mode,
  706. };
  707. static int vpbe_probe(struct platform_device *pdev)
  708. {
  709. struct vpbe_device *vpbe_dev;
  710. struct vpbe_config *cfg;
  711. if (!pdev->dev.platform_data) {
  712. v4l2_err(pdev->dev.driver, "No platform data\n");
  713. return -ENODEV;
  714. }
  715. cfg = pdev->dev.platform_data;
  716. if (!cfg->module_name[0] ||
  717. !cfg->osd.module_name[0] ||
  718. !cfg->venc.module_name[0]) {
  719. v4l2_err(pdev->dev.driver, "vpbe display module names not defined\n");
  720. return -EINVAL;
  721. }
  722. vpbe_dev = kzalloc(sizeof(*vpbe_dev), GFP_KERNEL);
  723. if (!vpbe_dev)
  724. return -ENOMEM;
  725. vpbe_dev->cfg = cfg;
  726. vpbe_dev->ops = vpbe_dev_ops;
  727. vpbe_dev->pdev = &pdev->dev;
  728. if (cfg->outputs->num_modes > 0)
  729. vpbe_dev->current_timings = vpbe_dev->cfg->outputs[0].modes[0];
  730. else {
  731. kfree(vpbe_dev);
  732. return -ENODEV;
  733. }
  734. /* set the driver data in platform device */
  735. platform_set_drvdata(pdev, vpbe_dev);
  736. mutex_init(&vpbe_dev->lock);
  737. return 0;
  738. }
  739. static int vpbe_remove(struct platform_device *device)
  740. {
  741. struct vpbe_device *vpbe_dev = platform_get_drvdata(device);
  742. kfree(vpbe_dev);
  743. return 0;
  744. }
  745. static struct platform_driver vpbe_driver = {
  746. .driver = {
  747. .name = "vpbe_controller",
  748. },
  749. .probe = vpbe_probe,
  750. .remove = vpbe_remove,
  751. };
  752. module_platform_driver(vpbe_driver);