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