via_aux_edid.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2011 Florian Tobias Schandinat <FlorianSchandinat@gmx.de>
  4. */
  5. /*
  6. * generic EDID driver
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/fb.h>
  10. #include "via_aux.h"
  11. #include "../edid.h"
  12. static const char *name = "EDID";
  13. static void query_edid(struct via_aux_drv *drv)
  14. {
  15. struct fb_monspecs *spec = drv->data;
  16. unsigned char edid[EDID_LENGTH];
  17. bool valid = false;
  18. if (spec) {
  19. fb_destroy_modedb(spec->modedb);
  20. } else {
  21. spec = kmalloc(sizeof(*spec), GFP_KERNEL);
  22. if (!spec)
  23. return;
  24. }
  25. spec->version = spec->revision = 0;
  26. if (via_aux_read(drv, 0x00, edid, EDID_LENGTH)) {
  27. fb_edid_to_monspecs(edid, spec);
  28. valid = spec->version || spec->revision;
  29. }
  30. if (!valid) {
  31. kfree(spec);
  32. spec = NULL;
  33. } else
  34. printk(KERN_DEBUG "EDID: %s %s\n", spec->manufacturer, spec->monitor);
  35. drv->data = spec;
  36. }
  37. static const struct fb_videomode *get_preferred_mode(struct via_aux_drv *drv)
  38. {
  39. struct fb_monspecs *spec = drv->data;
  40. int i;
  41. if (!spec || !spec->modedb || !(spec->misc & FB_MISC_1ST_DETAIL))
  42. return NULL;
  43. for (i = 0; i < spec->modedb_len; i++) {
  44. if (spec->modedb[i].flag & FB_MODE_IS_FIRST &&
  45. spec->modedb[i].flag & FB_MODE_IS_DETAILED)
  46. return &spec->modedb[i];
  47. }
  48. return NULL;
  49. }
  50. static void cleanup(struct via_aux_drv *drv)
  51. {
  52. struct fb_monspecs *spec = drv->data;
  53. if (spec)
  54. fb_destroy_modedb(spec->modedb);
  55. }
  56. void via_aux_edid_probe(struct via_aux_bus *bus)
  57. {
  58. struct via_aux_drv drv = {
  59. .bus = bus,
  60. .addr = 0x50,
  61. .name = name,
  62. .cleanup = cleanup,
  63. .get_preferred_mode = get_preferred_mode};
  64. query_edid(&drv);
  65. /* as EDID devices can be connected/disconnected just add the driver */
  66. via_aux_add(&drv);
  67. }