spmi.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <fdtdec.h>
  7. #include <dm.h>
  8. #include <malloc.h>
  9. #include <dm/device.h>
  10. #include <dm/root.h>
  11. #include <dm/test.h>
  12. #include <dm/util.h>
  13. #include <power/pmic.h>
  14. #include <spmi/spmi.h>
  15. #include <asm/gpio.h>
  16. #include <test/ut.h>
  17. /* Test if bus childs got probed propperly*/
  18. static int dm_test_spmi_probe(struct unit_test_state *uts)
  19. {
  20. const char *name = "spmi@0";
  21. struct udevice *bus, *dev;
  22. ut_assertok(uclass_get_device(UCLASS_SPMI, 0, &bus));
  23. /* Check bus name */
  24. ut_asserteq_str(name, bus->name);
  25. /* Check that it has some devices */
  26. ut_asserteq(device_has_children(bus), true);
  27. ut_assertok(device_find_first_child(bus, &dev));
  28. /* There should be at least one child */
  29. ut_assertnonnull(dev);
  30. /* Check that only PMICs are connected to the bus */
  31. while (dev) {
  32. ut_asserteq(device_get_uclass_id(dev), UCLASS_PMIC);
  33. device_find_next_child(&dev);
  34. }
  35. return 0;
  36. }
  37. DM_TEST(dm_test_spmi_probe, DM_TESTF_SCAN_FDT);
  38. /* Test if it's possible to read bus directly and indirectly */
  39. static int dm_test_spmi_access(struct unit_test_state *uts)
  40. {
  41. const char *pmic_name = "pm8916@0";
  42. struct udevice *bus, *pmic;
  43. ut_assertok(uclass_get_device(UCLASS_SPMI, 0, &bus));
  44. ut_assertok(device_get_child(bus, 0, &pmic));
  45. /* Sanity check if it's proper PMIC */
  46. ut_asserteq_str(pmic_name, pmic->name);
  47. /* Read PMIC ID reg using SPMI bus - it assumes it has slaveID == 0*/
  48. ut_asserteq(spmi_reg_read(bus, 0, 0xC0, 0x4), 0x10);
  49. ut_asserteq(spmi_reg_read(bus, 0, 0xC0, 0x5), 0x5);
  50. /* Read ID reg via pmic interface */
  51. ut_asserteq(pmic_reg_read(pmic, 0xC004), 0x10);
  52. ut_asserteq(pmic_reg_read(pmic, 0xC005), 0x5);
  53. return 0;
  54. }
  55. DM_TEST(dm_test_spmi_access, DM_TESTF_SCAN_FDT);
  56. /* Test if it's possible to access GPIO that should be in pmic */
  57. static int dm_test_spmi_access_peripheral(struct unit_test_state *uts)
  58. {
  59. struct udevice *dev;
  60. unsigned int offset, gpio;
  61. const char *name;
  62. int offset_count;
  63. /* Get second pin of PMIC GPIO */
  64. ut_assertok(gpio_lookup_name("spmi1", &dev, &offset, &gpio));
  65. /* Check if PMIC is parent */
  66. ut_asserteq(device_get_uclass_id(dev->parent), UCLASS_PMIC);
  67. /* This should be second gpio */
  68. ut_asserteq(1, offset);
  69. name = gpio_get_bank_info(dev, &offset_count);
  70. /* Check bank name */
  71. ut_asserteq_str("spmi", name);
  72. /* Check pin count */
  73. ut_asserteq(4, offset_count);
  74. ut_assertok(gpio_request(gpio, "testing"));
  75. /* Try to set/clear gpio */
  76. ut_assertok(gpio_direction_output(gpio, 0));
  77. ut_asserteq(gpio_get_value(gpio), 0);
  78. ut_assertok(gpio_direction_output(gpio, 1));
  79. ut_asserteq(gpio_get_value(gpio), 1);
  80. ut_assertok(gpio_direction_input(gpio));
  81. ut_asserteq(gpio_get_value(gpio), 1);
  82. ut_assertok(gpio_free(gpio));
  83. return 0;
  84. }
  85. DM_TEST(dm_test_spmi_access_peripheral, DM_TESTF_SCAN_FDT);