vpbe.c 23 KB

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