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