ps862x.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <i2c.h>
  11. #include <video_bridge.h>
  12. #include <power/regulator.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /*
  15. * Initialisation of the chip is a process of writing certain values into
  16. * certain registers over i2c bus. The chip in fact responds to a range of
  17. * addresses on the i2c bus, so for each written value three parameters are
  18. * required: i2c address, register address and the actual value.
  19. *
  20. * The base address is derived from the device tree, but oddly the chip
  21. * responds on several addresses with different register sets for each.
  22. */
  23. /**
  24. * ps8622_write() Write a PS8622 eDP bridge i2c register
  25. *
  26. * @param dev I2C device
  27. * @param addr_off offset from the i2c base address for ps8622
  28. * @param reg_addr register address to write
  29. * @param value value to be written
  30. * @return 0 on success, non-0 on failure
  31. */
  32. static int ps8622_write(struct udevice *dev, unsigned addr_off,
  33. unsigned char reg_addr, unsigned char value)
  34. {
  35. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  36. uint8_t buf[2];
  37. struct i2c_msg msg;
  38. int ret;
  39. msg.addr = chip->chip_addr + addr_off;
  40. msg.flags = 0;
  41. buf[0] = reg_addr;
  42. buf[1] = value;
  43. msg.buf = buf;
  44. msg.len = 2;
  45. ret = dm_i2c_xfer(dev, &msg, 1);
  46. if (ret) {
  47. debug("%s: write failed, reg=%#x, value=%#x, ret=%d\n",
  48. __func__, reg_addr, value, ret);
  49. return ret;
  50. }
  51. return 0;
  52. }
  53. static int ps8622_set_backlight(struct udevice *dev, int percent)
  54. {
  55. int level = percent * 255 / 100;
  56. debug("%s: level=%d\n", __func__, level);
  57. return ps8622_write(dev, 0x01, 0xa7, level);
  58. }
  59. static int ps8622_attach(struct udevice *dev)
  60. {
  61. const uint8_t *params;
  62. struct udevice *reg;
  63. int ret, i, len;
  64. debug("%s: %s\n", __func__, dev->name);
  65. /* set the LDO providing the 1.2V rail to the Parade bridge */
  66. ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
  67. "power-supply", &reg);
  68. if (!ret) {
  69. ret = regulator_autoset(reg);
  70. } else if (ret != -ENOENT) {
  71. debug("%s: Failed to enable power: ret=%d\n", __func__, ret);
  72. return ret;
  73. }
  74. ret = video_bridge_set_active(dev, true);
  75. if (ret)
  76. return ret;
  77. params = fdt_getprop(gd->fdt_blob, dev->of_offset, "parade,regs", &len);
  78. if (!params || len % 3) {
  79. debug("%s: missing/invalid params=%p, len=%x\n", __func__,
  80. params, len);
  81. return -EINVAL;
  82. }
  83. /* need to wait 20ms after power on before doing I2C writes */
  84. mdelay(20);
  85. for (i = 0; i < len; i += 3) {
  86. ret = ps8622_write(dev, params[i + 0], params[i + 1],
  87. params[i + 2]);
  88. if (ret)
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. static int ps8622_probe(struct udevice *dev)
  94. {
  95. debug("%s\n", __func__);
  96. if (device_get_uclass_id(dev->parent) != UCLASS_I2C)
  97. return -EPROTONOSUPPORT;
  98. return 0;
  99. }
  100. struct video_bridge_ops ps8622_ops = {
  101. .attach = ps8622_attach,
  102. .set_backlight = ps8622_set_backlight,
  103. };
  104. static const struct udevice_id ps8622_ids[] = {
  105. { .compatible = "parade,ps8622", },
  106. { .compatible = "parade,ps8625", },
  107. { }
  108. };
  109. U_BOOT_DRIVER(parade_ps8622) = {
  110. .name = "parade_ps8622",
  111. .id = UCLASS_VIDEO_BRIDGE,
  112. .of_match = ps8622_ids,
  113. .probe = ps8622_probe,
  114. .ops = &ps8622_ops,
  115. };