vpbe.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. * @cfg: ptr to vpbe cfg
  63. * @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. unsigned 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. * @dev: Master and slave device ptr
  493. * @vpbe_dev: vpbe device ptr
  494. *
  495. * Master frame buffer device drivers calls this to initialize vpbe
  496. * display controller. This will then registers v4l2 device and the sub
  497. * devices and sets a current encoder sub device for display. v4l2 display
  498. * device driver is the master and frame buffer display device driver is
  499. * the slave. Frame buffer display driver checks the initialized during
  500. * probe and exit if not initialized. Returns status.
  501. */
  502. static int vpbe_initialize(struct device *dev, struct vpbe_device *vpbe_dev)
  503. {
  504. struct encoder_config_info *enc_info;
  505. struct amp_config_info *amp_info;
  506. struct v4l2_subdev **enc_subdev;
  507. struct osd_state *osd_device;
  508. struct i2c_adapter *i2c_adap;
  509. int num_encoders;
  510. int ret = 0;
  511. int err;
  512. int i;
  513. /*
  514. * v4l2 abd FBDev frame buffer devices will get the vpbe_dev pointer
  515. * from the platform device by iteration of platform drivers and
  516. * matching with device name
  517. */
  518. if (!vpbe_dev || !dev) {
  519. printk(KERN_ERR "Null device pointers.\n");
  520. return -ENODEV;
  521. }
  522. if (vpbe_dev->initialized)
  523. return 0;
  524. mutex_lock(&vpbe_dev->lock);
  525. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  526. /* We have dac clock available for platform */
  527. vpbe_dev->dac_clk = clk_get(vpbe_dev->pdev, "vpss_dac");
  528. if (IS_ERR(vpbe_dev->dac_clk)) {
  529. ret = PTR_ERR(vpbe_dev->dac_clk);
  530. goto fail_mutex_unlock;
  531. }
  532. if (clk_prepare_enable(vpbe_dev->dac_clk)) {
  533. ret = -ENODEV;
  534. clk_put(vpbe_dev->dac_clk);
  535. goto fail_mutex_unlock;
  536. }
  537. }
  538. /* first enable vpss clocks */
  539. vpss_enable_clock(VPSS_VPBE_CLOCK, 1);
  540. /* First register a v4l2 device */
  541. ret = v4l2_device_register(dev, &vpbe_dev->v4l2_dev);
  542. if (ret) {
  543. v4l2_err(dev->driver,
  544. "Unable to register v4l2 device.\n");
  545. goto fail_clk_put;
  546. }
  547. v4l2_info(&vpbe_dev->v4l2_dev, "vpbe v4l2 device registered\n");
  548. err = bus_for_each_dev(&platform_bus_type, NULL, vpbe_dev,
  549. platform_device_get);
  550. if (err < 0) {
  551. ret = err;
  552. goto fail_dev_unregister;
  553. }
  554. vpbe_dev->venc = venc_sub_dev_init(&vpbe_dev->v4l2_dev,
  555. vpbe_dev->cfg->venc.module_name);
  556. /* register venc sub device */
  557. if (!vpbe_dev->venc) {
  558. v4l2_err(&vpbe_dev->v4l2_dev,
  559. "vpbe unable to init venc sub device\n");
  560. ret = -ENODEV;
  561. goto fail_dev_unregister;
  562. }
  563. /* initialize osd device */
  564. osd_device = vpbe_dev->osd_device;
  565. if (osd_device->ops.initialize) {
  566. err = osd_device->ops.initialize(osd_device);
  567. if (err) {
  568. v4l2_err(&vpbe_dev->v4l2_dev,
  569. "unable to initialize the OSD device");
  570. err = -ENOMEM;
  571. goto fail_dev_unregister;
  572. }
  573. }
  574. /*
  575. * Register any external encoders that are configured. At index 0 we
  576. * store venc sd index.
  577. */
  578. num_encoders = vpbe_dev->cfg->num_ext_encoders + 1;
  579. vpbe_dev->encoders = kmalloc_array(num_encoders,
  580. sizeof(*vpbe_dev->encoders),
  581. GFP_KERNEL);
  582. if (!vpbe_dev->encoders) {
  583. ret = -ENOMEM;
  584. goto fail_dev_unregister;
  585. }
  586. i2c_adap = i2c_get_adapter(vpbe_dev->cfg->i2c_adapter_id);
  587. for (i = 0; i < (vpbe_dev->cfg->num_ext_encoders + 1); i++) {
  588. if (i == 0) {
  589. /* venc is at index 0 */
  590. enc_subdev = &vpbe_dev->encoders[i];
  591. *enc_subdev = vpbe_dev->venc;
  592. continue;
  593. }
  594. enc_info = &vpbe_dev->cfg->ext_encoders[i];
  595. if (enc_info->is_i2c) {
  596. enc_subdev = &vpbe_dev->encoders[i];
  597. *enc_subdev = v4l2_i2c_new_subdev_board(
  598. &vpbe_dev->v4l2_dev, i2c_adap,
  599. &enc_info->board_info, NULL);
  600. if (*enc_subdev)
  601. v4l2_info(&vpbe_dev->v4l2_dev,
  602. "v4l2 sub device %s registered\n",
  603. enc_info->module_name);
  604. else {
  605. v4l2_err(&vpbe_dev->v4l2_dev, "encoder %s failed to register",
  606. enc_info->module_name);
  607. ret = -ENODEV;
  608. goto fail_kfree_encoders;
  609. }
  610. } else
  611. v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c encoders currently not supported");
  612. }
  613. /* Add amplifier subdevice for dm365 */
  614. if ((strcmp(vpbe_dev->cfg->module_name, "dm365-vpbe-display") == 0) &&
  615. vpbe_dev->cfg->amp) {
  616. amp_info = vpbe_dev->cfg->amp;
  617. if (amp_info->is_i2c) {
  618. vpbe_dev->amp = v4l2_i2c_new_subdev_board(
  619. &vpbe_dev->v4l2_dev, i2c_adap,
  620. &amp_info->board_info, NULL);
  621. if (!vpbe_dev->amp) {
  622. v4l2_err(&vpbe_dev->v4l2_dev,
  623. "amplifier %s failed to register",
  624. amp_info->module_name);
  625. ret = -ENODEV;
  626. goto fail_kfree_encoders;
  627. }
  628. v4l2_info(&vpbe_dev->v4l2_dev,
  629. "v4l2 sub device %s registered\n",
  630. amp_info->module_name);
  631. } else {
  632. vpbe_dev->amp = NULL;
  633. v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c amplifiers currently not supported");
  634. }
  635. } else {
  636. vpbe_dev->amp = NULL;
  637. }
  638. /* set the current encoder and output to that of venc by default */
  639. vpbe_dev->current_sd_index = 0;
  640. vpbe_dev->current_out_index = 0;
  641. mutex_unlock(&vpbe_dev->lock);
  642. printk(KERN_NOTICE "Setting default output to %s\n", def_output);
  643. ret = vpbe_set_default_output(vpbe_dev);
  644. if (ret) {
  645. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default output %s",
  646. def_output);
  647. goto fail_kfree_amp;
  648. }
  649. printk(KERN_NOTICE "Setting default mode to %s\n", def_mode);
  650. ret = vpbe_set_default_mode(vpbe_dev);
  651. if (ret) {
  652. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default mode %s",
  653. def_mode);
  654. goto fail_kfree_amp;
  655. }
  656. vpbe_dev->initialized = 1;
  657. /* TBD handling of bootargs for default output and mode */
  658. return 0;
  659. fail_kfree_amp:
  660. mutex_lock(&vpbe_dev->lock);
  661. kfree(vpbe_dev->amp);
  662. fail_kfree_encoders:
  663. kfree(vpbe_dev->encoders);
  664. fail_dev_unregister:
  665. v4l2_device_unregister(&vpbe_dev->v4l2_dev);
  666. fail_clk_put:
  667. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  668. clk_disable_unprepare(vpbe_dev->dac_clk);
  669. clk_put(vpbe_dev->dac_clk);
  670. }
  671. fail_mutex_unlock:
  672. mutex_unlock(&vpbe_dev->lock);
  673. return ret;
  674. }
  675. /**
  676. * vpbe_deinitialize() - de-initialize the vpbe display controller
  677. * @dev: Master and slave device ptr
  678. * @vpbe_dev: vpbe device ptr
  679. *
  680. * vpbe_master and slave frame buffer devices calls this to de-initialize
  681. * the display controller. It is called when master and slave device
  682. * driver modules are removed and no longer requires the display controller.
  683. */
  684. static void vpbe_deinitialize(struct device *dev, struct vpbe_device *vpbe_dev)
  685. {
  686. v4l2_device_unregister(&vpbe_dev->v4l2_dev);
  687. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  688. clk_disable_unprepare(vpbe_dev->dac_clk);
  689. clk_put(vpbe_dev->dac_clk);
  690. }
  691. kfree(vpbe_dev->amp);
  692. kfree(vpbe_dev->encoders);
  693. vpbe_dev->initialized = 0;
  694. /* disable vpss clocks */
  695. vpss_enable_clock(VPSS_VPBE_CLOCK, 0);
  696. }
  697. static const struct vpbe_device_ops vpbe_dev_ops = {
  698. .g_cropcap = vpbe_g_cropcap,
  699. .enum_outputs = vpbe_enum_outputs,
  700. .set_output = vpbe_set_output,
  701. .get_output = vpbe_get_output,
  702. .s_dv_timings = vpbe_s_dv_timings,
  703. .g_dv_timings = vpbe_g_dv_timings,
  704. .enum_dv_timings = vpbe_enum_dv_timings,
  705. .s_std = vpbe_s_std,
  706. .g_std = vpbe_g_std,
  707. .initialize = vpbe_initialize,
  708. .deinitialize = vpbe_deinitialize,
  709. .get_mode_info = vpbe_get_current_mode_info,
  710. .set_mode = vpbe_set_mode,
  711. };
  712. static int vpbe_probe(struct platform_device *pdev)
  713. {
  714. struct vpbe_device *vpbe_dev;
  715. struct vpbe_config *cfg;
  716. if (!pdev->dev.platform_data) {
  717. v4l2_err(pdev->dev.driver, "No platform data\n");
  718. return -ENODEV;
  719. }
  720. cfg = pdev->dev.platform_data;
  721. if (!cfg->module_name[0] ||
  722. !cfg->osd.module_name[0] ||
  723. !cfg->venc.module_name[0]) {
  724. v4l2_err(pdev->dev.driver, "vpbe display module names not defined\n");
  725. return -EINVAL;
  726. }
  727. vpbe_dev = kzalloc(sizeof(*vpbe_dev), GFP_KERNEL);
  728. if (!vpbe_dev)
  729. return -ENOMEM;
  730. vpbe_dev->cfg = cfg;
  731. vpbe_dev->ops = vpbe_dev_ops;
  732. vpbe_dev->pdev = &pdev->dev;
  733. if (cfg->outputs->num_modes > 0)
  734. vpbe_dev->current_timings = vpbe_dev->cfg->outputs[0].modes[0];
  735. else {
  736. kfree(vpbe_dev);
  737. return -ENODEV;
  738. }
  739. /* set the driver data in platform device */
  740. platform_set_drvdata(pdev, vpbe_dev);
  741. mutex_init(&vpbe_dev->lock);
  742. return 0;
  743. }
  744. static int vpbe_remove(struct platform_device *device)
  745. {
  746. struct vpbe_device *vpbe_dev = platform_get_drvdata(device);
  747. kfree(vpbe_dev);
  748. return 0;
  749. }
  750. static struct platform_driver vpbe_driver = {
  751. .driver = {
  752. .name = "vpbe_controller",
  753. },
  754. .probe = vpbe_probe,
  755. .remove = vpbe_remove,
  756. };
  757. module_platform_driver(vpbe_driver);