vpbe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. {
  127. struct vpbe_config *cfg = vpbe_dev->cfg;
  128. struct vpbe_enc_mode_info var;
  129. int curr_output = vpbe_dev->current_out_index;
  130. int i;
  131. if (NULL == mode)
  132. return -EINVAL;
  133. for (i = 0; i < cfg->outputs[curr_output].num_modes; i++) {
  134. var = cfg->outputs[curr_output].modes[i];
  135. if (!strcmp(mode, var.name)) {
  136. vpbe_dev->current_timings = var;
  137. return 0;
  138. }
  139. }
  140. return -EINVAL;
  141. }
  142. static int vpbe_get_current_mode_info(struct vpbe_device *vpbe_dev,
  143. struct vpbe_enc_mode_info *mode_info)
  144. {
  145. if (NULL == mode_info)
  146. return -EINVAL;
  147. *mode_info = vpbe_dev->current_timings;
  148. return 0;
  149. }
  150. static int vpbe_get_dv_preset_info(struct vpbe_device *vpbe_dev,
  151. unsigned int dv_preset)
  152. {
  153. struct vpbe_config *cfg = vpbe_dev->cfg;
  154. struct vpbe_enc_mode_info var;
  155. int curr_output = vpbe_dev->current_out_index;
  156. int i;
  157. for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
  158. var = cfg->outputs[curr_output].modes[i];
  159. if ((var.timings_type & VPBE_ENC_DV_PRESET) &&
  160. (var.timings.dv_preset == dv_preset)) {
  161. vpbe_dev->current_timings = var;
  162. return 0;
  163. }
  164. }
  165. return -EINVAL;
  166. }
  167. /* Get std by std id */
  168. static int vpbe_get_std_info(struct vpbe_device *vpbe_dev,
  169. v4l2_std_id std_id)
  170. {
  171. struct vpbe_config *cfg = vpbe_dev->cfg;
  172. struct vpbe_enc_mode_info var;
  173. int curr_output = vpbe_dev->current_out_index;
  174. int i;
  175. for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
  176. var = cfg->outputs[curr_output].modes[i];
  177. if ((var.timings_type & VPBE_ENC_STD) &&
  178. (var.timings.std_id & std_id)) {
  179. vpbe_dev->current_timings = var;
  180. return 0;
  181. }
  182. }
  183. return -EINVAL;
  184. }
  185. static int vpbe_get_std_info_by_name(struct vpbe_device *vpbe_dev,
  186. char *std_name)
  187. {
  188. struct vpbe_config *cfg = vpbe_dev->cfg;
  189. struct vpbe_enc_mode_info var;
  190. int curr_output = vpbe_dev->current_out_index;
  191. int i;
  192. for (i = 0; i < vpbe_dev->cfg->outputs[curr_output].num_modes; i++) {
  193. var = cfg->outputs[curr_output].modes[i];
  194. if (!strcmp(var.name, std_name)) {
  195. vpbe_dev->current_timings = var;
  196. return 0;
  197. }
  198. }
  199. return -EINVAL;
  200. }
  201. /**
  202. * vpbe_set_output - Set output
  203. * @vpbe_dev - vpbe device ptr
  204. * @index - index of output
  205. *
  206. * Set vpbe output to the output specified by the index
  207. */
  208. static int vpbe_set_output(struct vpbe_device *vpbe_dev, int index)
  209. {
  210. struct encoder_config_info *curr_enc_info =
  211. vpbe_current_encoder_info(vpbe_dev);
  212. struct vpbe_config *cfg = vpbe_dev->cfg;
  213. int enc_out_index;
  214. int sd_index;
  215. int ret = 0;
  216. if (index >= cfg->num_outputs)
  217. return -EINVAL;
  218. mutex_lock(&vpbe_dev->lock);
  219. sd_index = vpbe_dev->current_sd_index;
  220. enc_out_index = cfg->outputs[index].output.index;
  221. /*
  222. * Currently we switch the encoder based on output selected
  223. * by the application. If media controller is implemented later
  224. * there is will be an API added to setup_link between venc
  225. * and external encoder. So in that case below comparison always
  226. * match and encoder will not be switched. But if application
  227. * chose not to use media controller, then this provides current
  228. * way of switching encoder at the venc output.
  229. */
  230. if (strcmp(curr_enc_info->module_name,
  231. cfg->outputs[index].subdev_name)) {
  232. /* Need to switch the encoder at the output */
  233. sd_index = vpbe_find_encoder_sd_index(cfg, index);
  234. if (sd_index < 0) {
  235. ret = -EINVAL;
  236. goto out;
  237. }
  238. if (ret)
  239. goto out;
  240. }
  241. /* Set output at the encoder */
  242. ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
  243. s_routing, 0, enc_out_index, 0);
  244. if (ret)
  245. goto out;
  246. /*
  247. * It is assumed that venc or extenal encoder will set a default
  248. * mode in the sub device. For external encoder or LCD pannel output,
  249. * we also need to set up the lcd port for the required mode. So setup
  250. * the lcd port for the default mode that is configured in the board
  251. * arch/arm/mach-davinci/board-dm355-evm.setup file for the external
  252. * encoder.
  253. */
  254. ret = vpbe_get_mode_info(vpbe_dev,
  255. cfg->outputs[index].default_mode);
  256. if (!ret) {
  257. struct osd_state *osd_device = vpbe_dev->osd_device;
  258. osd_device->ops.set_left_margin(osd_device,
  259. vpbe_dev->current_timings.left_margin);
  260. osd_device->ops.set_top_margin(osd_device,
  261. vpbe_dev->current_timings.upper_margin);
  262. vpbe_dev->current_sd_index = sd_index;
  263. vpbe_dev->current_out_index = index;
  264. }
  265. out:
  266. mutex_unlock(&vpbe_dev->lock);
  267. return ret;
  268. }
  269. static int vpbe_set_default_output(struct vpbe_device *vpbe_dev)
  270. {
  271. struct vpbe_config *cfg = vpbe_dev->cfg;
  272. int ret = 0;
  273. int i;
  274. for (i = 0; i < cfg->num_outputs; i++) {
  275. if (!strcmp(def_output,
  276. cfg->outputs[i].output.name)) {
  277. ret = vpbe_set_output(vpbe_dev, i);
  278. if (!ret)
  279. vpbe_dev->current_out_index = i;
  280. return ret;
  281. }
  282. }
  283. return ret;
  284. }
  285. /**
  286. * vpbe_get_output - Get output
  287. * @vpbe_dev - vpbe device ptr
  288. *
  289. * return current vpbe output to the the index
  290. */
  291. static unsigned int vpbe_get_output(struct vpbe_device *vpbe_dev)
  292. {
  293. return vpbe_dev->current_out_index;
  294. }
  295. /**
  296. * vpbe_s_dv_preset - Set the given preset timings in the encoder
  297. *
  298. * Sets the preset if supported by the current encoder. Return the status.
  299. * 0 - success & -EINVAL on error
  300. */
  301. static int vpbe_s_dv_preset(struct vpbe_device *vpbe_dev,
  302. struct v4l2_dv_preset *dv_preset)
  303. {
  304. struct vpbe_config *cfg = vpbe_dev->cfg;
  305. int out_index = vpbe_dev->current_out_index;
  306. int sd_index = vpbe_dev->current_sd_index;
  307. int ret;
  308. if (!(cfg->outputs[out_index].output.capabilities &
  309. V4L2_OUT_CAP_PRESETS))
  310. return -EINVAL;
  311. ret = vpbe_get_dv_preset_info(vpbe_dev, dv_preset->preset);
  312. if (ret)
  313. return ret;
  314. mutex_lock(&vpbe_dev->lock);
  315. ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
  316. s_dv_preset, dv_preset);
  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_preset - Get the preset in the current encoder
  330. *
  331. * Get the preset in the current encoder. Return the status. 0 - success
  332. * -EINVAL on error
  333. */
  334. static int vpbe_g_dv_preset(struct vpbe_device *vpbe_dev,
  335. struct v4l2_dv_preset *dv_preset)
  336. {
  337. if (vpbe_dev->current_timings.timings_type &
  338. VPBE_ENC_DV_PRESET) {
  339. dv_preset->preset = vpbe_dev->current_timings.timings.dv_preset;
  340. return 0;
  341. }
  342. return -EINVAL;
  343. }
  344. /**
  345. * vpbe_enum_dv_presets - Enumerate the dv presets in the current encoder
  346. *
  347. * Get the preset in the current encoder. Return the status. 0 - success
  348. * -EINVAL on error
  349. */
  350. static int vpbe_enum_dv_presets(struct vpbe_device *vpbe_dev,
  351. struct v4l2_dv_enum_preset *preset_info)
  352. {
  353. struct vpbe_config *cfg = vpbe_dev->cfg;
  354. int out_index = vpbe_dev->current_out_index;
  355. struct vpbe_output *output = &cfg->outputs[out_index];
  356. int j = 0;
  357. int i;
  358. if (!(output->output.capabilities & V4L2_OUT_CAP_PRESETS))
  359. return -EINVAL;
  360. for (i = 0; i < output->num_modes; i++) {
  361. if (output->modes[i].timings_type == VPBE_ENC_DV_PRESET) {
  362. if (j == preset_info->index)
  363. break;
  364. j++;
  365. }
  366. }
  367. if (i == output->num_modes)
  368. return -EINVAL;
  369. return v4l_fill_dv_preset_info(output->modes[i].timings.dv_preset,
  370. preset_info);
  371. }
  372. /**
  373. * vpbe_s_std - Set the given standard in the encoder
  374. *
  375. * Sets the standard if supported by the current encoder. Return the status.
  376. * 0 - success & -EINVAL on error
  377. */
  378. static int vpbe_s_std(struct vpbe_device *vpbe_dev, v4l2_std_id *std_id)
  379. {
  380. struct vpbe_config *cfg = vpbe_dev->cfg;
  381. int out_index = vpbe_dev->current_out_index;
  382. int sd_index = vpbe_dev->current_sd_index;
  383. int ret;
  384. if (!(cfg->outputs[out_index].output.capabilities &
  385. V4L2_OUT_CAP_STD))
  386. return -EINVAL;
  387. ret = vpbe_get_std_info(vpbe_dev, *std_id);
  388. if (ret)
  389. return ret;
  390. mutex_lock(&vpbe_dev->lock);
  391. ret = v4l2_subdev_call(vpbe_dev->encoders[sd_index], video,
  392. s_std_output, *std_id);
  393. /* set the lcd controller output for the given mode */
  394. if (!ret) {
  395. struct osd_state *osd_device = vpbe_dev->osd_device;
  396. osd_device->ops.set_left_margin(osd_device,
  397. vpbe_dev->current_timings.left_margin);
  398. osd_device->ops.set_top_margin(osd_device,
  399. vpbe_dev->current_timings.upper_margin);
  400. }
  401. mutex_unlock(&vpbe_dev->lock);
  402. return ret;
  403. }
  404. /**
  405. * vpbe_g_std - Get the standard in the current encoder
  406. *
  407. * Get the standard in the current encoder. Return the status. 0 - success
  408. * -EINVAL on error
  409. */
  410. static int vpbe_g_std(struct vpbe_device *vpbe_dev, v4l2_std_id *std_id)
  411. {
  412. struct vpbe_enc_mode_info cur_timings = vpbe_dev->current_timings;
  413. if (cur_timings.timings_type & VPBE_ENC_STD) {
  414. *std_id = cur_timings.timings.std_id;
  415. return 0;
  416. }
  417. return -EINVAL;
  418. }
  419. /**
  420. * vpbe_set_mode - Set mode in the current encoder using mode info
  421. *
  422. * Use the mode string to decide what timings to set in the encoder
  423. * This is typically useful when fbset command is used to change the current
  424. * timings by specifying a string to indicate the timings.
  425. */
  426. static int vpbe_set_mode(struct vpbe_device *vpbe_dev,
  427. struct vpbe_enc_mode_info *mode_info)
  428. {
  429. struct vpbe_enc_mode_info *preset_mode = NULL;
  430. struct vpbe_config *cfg = vpbe_dev->cfg;
  431. struct v4l2_dv_preset dv_preset;
  432. struct osd_state *osd_device;
  433. int out_index = vpbe_dev->current_out_index;
  434. int ret = 0;
  435. int i;
  436. if ((NULL == mode_info) || (NULL == mode_info->name))
  437. return -EINVAL;
  438. for (i = 0; i < cfg->outputs[out_index].num_modes; i++) {
  439. if (!strcmp(mode_info->name,
  440. cfg->outputs[out_index].modes[i].name)) {
  441. preset_mode = &cfg->outputs[out_index].modes[i];
  442. /*
  443. * it may be one of the 3 timings type. Check and
  444. * invoke right API
  445. */
  446. if (preset_mode->timings_type & VPBE_ENC_STD)
  447. return vpbe_s_std(vpbe_dev,
  448. &preset_mode->timings.std_id);
  449. if (preset_mode->timings_type & VPBE_ENC_DV_PRESET) {
  450. dv_preset.preset =
  451. preset_mode->timings.dv_preset;
  452. return vpbe_s_dv_preset(vpbe_dev, &dv_preset);
  453. }
  454. }
  455. }
  456. /* Only custom timing should reach here */
  457. if (preset_mode == NULL)
  458. return -EINVAL;
  459. mutex_lock(&vpbe_dev->lock);
  460. osd_device = vpbe_dev->osd_device;
  461. vpbe_dev->current_timings = *preset_mode;
  462. osd_device->ops.set_left_margin(osd_device,
  463. vpbe_dev->current_timings.left_margin);
  464. osd_device->ops.set_top_margin(osd_device,
  465. vpbe_dev->current_timings.upper_margin);
  466. mutex_unlock(&vpbe_dev->lock);
  467. return ret;
  468. }
  469. static int vpbe_set_default_mode(struct vpbe_device *vpbe_dev)
  470. {
  471. int ret;
  472. ret = vpbe_get_std_info_by_name(vpbe_dev, def_mode);
  473. if (ret)
  474. return ret;
  475. /* set the default mode in the encoder */
  476. return vpbe_set_mode(vpbe_dev, &vpbe_dev->current_timings);
  477. }
  478. static int platform_device_get(struct device *dev, void *data)
  479. {
  480. struct platform_device *pdev = to_platform_device(dev);
  481. struct vpbe_device *vpbe_dev = data;
  482. if (strcmp("vpbe-osd", pdev->name) == 0)
  483. vpbe_dev->osd_device = platform_get_drvdata(pdev);
  484. return 0;
  485. }
  486. /**
  487. * vpbe_initialize() - Initialize the vpbe display controller
  488. * @vpbe_dev - vpbe device ptr
  489. *
  490. * Master frame buffer device drivers calls this to initialize vpbe
  491. * display controller. This will then registers v4l2 device and the sub
  492. * devices and sets a current encoder sub device for display. v4l2 display
  493. * device driver is the master and frame buffer display device driver is
  494. * the slave. Frame buffer display driver checks the initialized during
  495. * probe and exit if not initialized. Returns status.
  496. */
  497. static int vpbe_initialize(struct device *dev, struct vpbe_device *vpbe_dev)
  498. {
  499. struct encoder_config_info *enc_info;
  500. struct v4l2_subdev **enc_subdev;
  501. struct osd_state *osd_device;
  502. struct i2c_adapter *i2c_adap;
  503. int output_index;
  504. int num_encoders;
  505. int ret = 0;
  506. int err;
  507. int i;
  508. /*
  509. * v4l2 abd FBDev frame buffer devices will get the vpbe_dev pointer
  510. * from the platform device by iteration of platform drivers and
  511. * matching with device name
  512. */
  513. if (NULL == vpbe_dev || NULL == dev) {
  514. printk(KERN_ERR "Null device pointers.\n");
  515. return -ENODEV;
  516. }
  517. if (vpbe_dev->initialized)
  518. return 0;
  519. mutex_lock(&vpbe_dev->lock);
  520. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0) {
  521. /* We have dac clock available for platform */
  522. vpbe_dev->dac_clk = clk_get(vpbe_dev->pdev, "vpss_dac");
  523. if (IS_ERR(vpbe_dev->dac_clk)) {
  524. ret = PTR_ERR(vpbe_dev->dac_clk);
  525. goto vpbe_unlock;
  526. }
  527. if (clk_enable(vpbe_dev->dac_clk)) {
  528. ret = -ENODEV;
  529. goto vpbe_unlock;
  530. }
  531. }
  532. /* first enable vpss clocks */
  533. vpss_enable_clock(VPSS_VPBE_CLOCK, 1);
  534. /* First register a v4l2 device */
  535. ret = v4l2_device_register(dev, &vpbe_dev->v4l2_dev);
  536. if (ret) {
  537. v4l2_err(dev->driver,
  538. "Unable to register v4l2 device.\n");
  539. goto vpbe_fail_clock;
  540. }
  541. v4l2_info(&vpbe_dev->v4l2_dev, "vpbe v4l2 device registered\n");
  542. err = bus_for_each_dev(&platform_bus_type, NULL, vpbe_dev,
  543. platform_device_get);
  544. if (err < 0)
  545. return err;
  546. vpbe_dev->venc = venc_sub_dev_init(&vpbe_dev->v4l2_dev,
  547. vpbe_dev->cfg->venc.module_name);
  548. /* register venc sub device */
  549. if (vpbe_dev->venc == NULL) {
  550. v4l2_err(&vpbe_dev->v4l2_dev,
  551. "vpbe unable to init venc sub device\n");
  552. ret = -ENODEV;
  553. goto vpbe_fail_v4l2_device;
  554. }
  555. /* initialize osd device */
  556. osd_device = vpbe_dev->osd_device;
  557. if (NULL != osd_device->ops.initialize) {
  558. err = osd_device->ops.initialize(osd_device);
  559. if (err) {
  560. v4l2_err(&vpbe_dev->v4l2_dev,
  561. "unable to initialize the OSD device");
  562. err = -ENOMEM;
  563. goto vpbe_fail_v4l2_device;
  564. }
  565. }
  566. /*
  567. * Register any external encoders that are configured. At index 0 we
  568. * store venc sd index.
  569. */
  570. num_encoders = vpbe_dev->cfg->num_ext_encoders + 1;
  571. vpbe_dev->encoders = kmalloc(
  572. sizeof(struct v4l2_subdev *)*num_encoders,
  573. GFP_KERNEL);
  574. if (NULL == vpbe_dev->encoders) {
  575. v4l2_err(&vpbe_dev->v4l2_dev,
  576. "unable to allocate memory for encoders sub devices");
  577. ret = -ENOMEM;
  578. goto vpbe_fail_v4l2_device;
  579. }
  580. i2c_adap = i2c_get_adapter(vpbe_dev->cfg->i2c_adapter_id);
  581. for (i = 0; i < (vpbe_dev->cfg->num_ext_encoders + 1); i++) {
  582. if (i == 0) {
  583. /* venc is at index 0 */
  584. enc_subdev = &vpbe_dev->encoders[i];
  585. *enc_subdev = vpbe_dev->venc;
  586. continue;
  587. }
  588. enc_info = &vpbe_dev->cfg->ext_encoders[i];
  589. if (enc_info->is_i2c) {
  590. enc_subdev = &vpbe_dev->encoders[i];
  591. *enc_subdev = v4l2_i2c_new_subdev_board(
  592. &vpbe_dev->v4l2_dev, i2c_adap,
  593. &enc_info->board_info, NULL);
  594. if (*enc_subdev)
  595. v4l2_info(&vpbe_dev->v4l2_dev,
  596. "v4l2 sub device %s registered\n",
  597. enc_info->module_name);
  598. else {
  599. v4l2_err(&vpbe_dev->v4l2_dev, "encoder %s"
  600. " failed to register",
  601. enc_info->module_name);
  602. ret = -ENODEV;
  603. goto vpbe_fail_sd_register;
  604. }
  605. } else
  606. v4l2_warn(&vpbe_dev->v4l2_dev, "non-i2c encoders"
  607. " currently not supported");
  608. }
  609. /* set the current encoder and output to that of venc by default */
  610. vpbe_dev->current_sd_index = 0;
  611. vpbe_dev->current_out_index = 0;
  612. output_index = 0;
  613. mutex_unlock(&vpbe_dev->lock);
  614. printk(KERN_NOTICE "Setting default output to %s\n", def_output);
  615. ret = vpbe_set_default_output(vpbe_dev);
  616. if (ret) {
  617. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default output %s",
  618. def_output);
  619. return ret;
  620. }
  621. printk(KERN_NOTICE "Setting default mode to %s\n", def_mode);
  622. ret = vpbe_set_default_mode(vpbe_dev);
  623. if (ret) {
  624. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to set default mode %s",
  625. def_mode);
  626. return ret;
  627. }
  628. vpbe_dev->initialized = 1;
  629. /* TBD handling of bootargs for default output and mode */
  630. return 0;
  631. vpbe_fail_sd_register:
  632. kfree(vpbe_dev->encoders);
  633. vpbe_fail_v4l2_device:
  634. v4l2_device_unregister(&vpbe_dev->v4l2_dev);
  635. vpbe_fail_clock:
  636. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0)
  637. clk_put(vpbe_dev->dac_clk);
  638. vpbe_unlock:
  639. mutex_unlock(&vpbe_dev->lock);
  640. return ret;
  641. }
  642. /**
  643. * vpbe_deinitialize() - de-initialize the vpbe display controller
  644. * @dev - Master and slave device ptr
  645. *
  646. * vpbe_master and slave frame buffer devices calls this to de-initialize
  647. * the display controller. It is called when master and slave device
  648. * driver modules are removed and no longer requires the display controller.
  649. */
  650. static void vpbe_deinitialize(struct device *dev, struct vpbe_device *vpbe_dev)
  651. {
  652. v4l2_device_unregister(&vpbe_dev->v4l2_dev);
  653. if (strcmp(vpbe_dev->cfg->module_name, "dm644x-vpbe-display") != 0)
  654. clk_put(vpbe_dev->dac_clk);
  655. kfree(vpbe_dev->encoders);
  656. vpbe_dev->initialized = 0;
  657. /* disable vpss clocks */
  658. vpss_enable_clock(VPSS_VPBE_CLOCK, 0);
  659. }
  660. static struct vpbe_device_ops vpbe_dev_ops = {
  661. .g_cropcap = vpbe_g_cropcap,
  662. .enum_outputs = vpbe_enum_outputs,
  663. .set_output = vpbe_set_output,
  664. .get_output = vpbe_get_output,
  665. .s_dv_preset = vpbe_s_dv_preset,
  666. .g_dv_preset = vpbe_g_dv_preset,
  667. .enum_dv_presets = vpbe_enum_dv_presets,
  668. .s_std = vpbe_s_std,
  669. .g_std = vpbe_g_std,
  670. .initialize = vpbe_initialize,
  671. .deinitialize = vpbe_deinitialize,
  672. .get_mode_info = vpbe_get_current_mode_info,
  673. .set_mode = vpbe_set_mode,
  674. };
  675. static __devinit int vpbe_probe(struct platform_device *pdev)
  676. {
  677. struct vpbe_device *vpbe_dev;
  678. struct vpbe_config *cfg;
  679. int ret = -EINVAL;
  680. if (pdev->dev.platform_data == NULL) {
  681. v4l2_err(pdev->dev.driver, "No platform data\n");
  682. return -ENODEV;
  683. }
  684. cfg = pdev->dev.platform_data;
  685. if (!cfg->module_name[0] ||
  686. !cfg->osd.module_name[0] ||
  687. !cfg->venc.module_name[0]) {
  688. v4l2_err(pdev->dev.driver, "vpbe display module names not"
  689. " defined\n");
  690. return ret;
  691. }
  692. vpbe_dev = kzalloc(sizeof(*vpbe_dev), GFP_KERNEL);
  693. if (vpbe_dev == NULL) {
  694. v4l2_err(pdev->dev.driver, "Unable to allocate memory"
  695. " for vpbe_device\n");
  696. return -ENOMEM;
  697. }
  698. vpbe_dev->cfg = cfg;
  699. vpbe_dev->ops = vpbe_dev_ops;
  700. vpbe_dev->pdev = &pdev->dev;
  701. if (cfg->outputs->num_modes > 0)
  702. vpbe_dev->current_timings = vpbe_dev->cfg->outputs[0].modes[0];
  703. else
  704. return -ENODEV;
  705. /* set the driver data in platform device */
  706. platform_set_drvdata(pdev, vpbe_dev);
  707. mutex_init(&vpbe_dev->lock);
  708. return 0;
  709. }
  710. static int vpbe_remove(struct platform_device *device)
  711. {
  712. struct vpbe_device *vpbe_dev = platform_get_drvdata(device);
  713. kfree(vpbe_dev);
  714. return 0;
  715. }
  716. static struct platform_driver vpbe_driver = {
  717. .driver = {
  718. .name = "vpbe_controller",
  719. .owner = THIS_MODULE,
  720. },
  721. .probe = vpbe_probe,
  722. .remove = vpbe_remove,
  723. };
  724. /**
  725. * vpbe_init: initialize the vpbe driver
  726. *
  727. * This function registers device and driver to the kernel
  728. */
  729. static __init int vpbe_init(void)
  730. {
  731. return platform_driver_register(&vpbe_driver);
  732. }
  733. /**
  734. * vpbe_cleanup : cleanup function for vpbe driver
  735. *
  736. * This will un-registers the device and driver to the kernel
  737. */
  738. static void vpbe_cleanup(void)
  739. {
  740. platform_driver_unregister(&vpbe_driver);
  741. }
  742. /* Function for module initialization and cleanup */
  743. module_init(vpbe_init);
  744. module_exit(vpbe_cleanup);