|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <i2c.h> |
| 8 | +#include <zephyr.h> |
| 9 | +#include <ztest.h> |
| 10 | + |
| 11 | +/* |
| 12 | + * For ADV7513 Programming details, please |
| 13 | + * refer to the following link. |
| 14 | + * https://ez.analog.com/docs/DOC-1986 |
| 15 | + */ |
| 16 | + |
| 17 | +#define ADV7513_HDMI_I2C_SLAVE_ADDR 0x39 |
| 18 | + |
| 19 | +#define ADV7513_CHIP_REVISION_REG 0x0 |
| 20 | +#define CHIP_REVISION_VAL 0x13 |
| 21 | + |
| 22 | +#define ADV7513_MAIN_POWER_REG 0x41 |
| 23 | +#define POWER_ON_VAL 0x10 |
| 24 | + |
| 25 | +#define ADV7513_HPD_CTRL_REG 0xD6 |
| 26 | +#define HPD_CTRL_VAL 0xC0 |
| 27 | + |
| 28 | +#define ADV7513_WRITE_TEST_REG 0x2 |
| 29 | +#define WRITE_TEST_VAL 0x66 |
| 30 | + |
| 31 | +static int powerup_adv7513(struct device *i2c_dev) |
| 32 | +{ |
| 33 | + u8_t data; |
| 34 | + |
| 35 | + TC_PRINT("Powering up ADV7513\n"); |
| 36 | + /* write to HPD control registers */ |
| 37 | + if (i2c_reg_write_byte(i2c_dev, ADV7513_HDMI_I2C_SLAVE_ADDR, |
| 38 | + ADV7513_HPD_CTRL_REG, HPD_CTRL_VAL)) { |
| 39 | + TC_PRINT("i2c write fail\n"); |
| 40 | + return TC_FAIL; |
| 41 | + } |
| 42 | + if (i2c_reg_read_byte(i2c_dev, ADV7513_HDMI_I2C_SLAVE_ADDR, |
| 43 | + 0xD6, &data)) { |
| 44 | + TC_PRINT("failed to read HPD control\n"); |
| 45 | + return TC_FAIL; |
| 46 | + } |
| 47 | + TC_PRINT("HPD control 0x%x\n", data); |
| 48 | + |
| 49 | + /* write to power control registers */ |
| 50 | + if (i2c_reg_write_byte(i2c_dev, ADV7513_HDMI_I2C_SLAVE_ADDR, |
| 51 | + ADV7513_MAIN_POWER_REG, POWER_ON_VAL)) { |
| 52 | + TC_PRINT("i2c write fail\n"); |
| 53 | + return TC_FAIL; |
| 54 | + } |
| 55 | + |
| 56 | + if (i2c_reg_read_byte(i2c_dev, ADV7513_HDMI_I2C_SLAVE_ADDR, |
| 57 | + 0x41, &data)) { |
| 58 | + TC_PRINT("failed to read Power state\n"); |
| 59 | + return TC_FAIL; |
| 60 | + } |
| 61 | + TC_PRINT("Power state 0x%x\n", data); |
| 62 | + |
| 63 | + return TC_PASS; |
| 64 | +} |
| 65 | + |
| 66 | +static int test_i2c_adv7513(void) |
| 67 | +{ |
| 68 | + struct device *i2c_dev = device_get_binding(CONFIG_I2C_0_NAME); |
| 69 | + u32_t i2c_cfg = I2C_SPEED_SET(I2C_SPEED_STANDARD) | I2C_MODE_MASTER; |
| 70 | + u8_t data; |
| 71 | + |
| 72 | + if (!i2c_dev) { |
| 73 | + TC_PRINT("cannot get i2c device\n"); |
| 74 | + return TC_FAIL; |
| 75 | + } |
| 76 | + |
| 77 | + /* Test i2c_configure() */ |
| 78 | + if (i2c_configure(i2c_dev, i2c_cfg)) { |
| 79 | + TC_PRINT("i2c config failed\n"); |
| 80 | + return TC_FAIL; |
| 81 | + } |
| 82 | + |
| 83 | + /* Power up ADV7513 */ |
| 84 | + zassert_true(powerup_adv7513(i2c_dev) == TC_PASS, |
| 85 | + "ADV7513 power up failed"); |
| 86 | + |
| 87 | + TC_PRINT("*** Running i2c read/write tests ***\n"); |
| 88 | + /* Test i2c byte read */ |
| 89 | + data = 0x0; |
| 90 | + if (i2c_reg_read_byte(i2c_dev, ADV7513_HDMI_I2C_SLAVE_ADDR, |
| 91 | + ADV7513_CHIP_REVISION_REG, &data)) { |
| 92 | + TC_PRINT("failed to read chip revision\n"); |
| 93 | + return TC_FAIL; |
| 94 | + } |
| 95 | + if (data != CHIP_REVISION_VAL) { |
| 96 | + TC_PRINT("chip revision does not match 0x%x\n", data); |
| 97 | + return TC_FAIL; |
| 98 | + } |
| 99 | + TC_PRINT("i2c read test passed\n"); |
| 100 | + |
| 101 | + |
| 102 | + /* Test i2c byte write */ |
| 103 | + data = WRITE_TEST_VAL; |
| 104 | + if (i2c_reg_write_byte(i2c_dev, ADV7513_HDMI_I2C_SLAVE_ADDR, |
| 105 | + ADV7513_WRITE_TEST_REG, data)) { |
| 106 | + TC_PRINT("i2c write fail\n"); |
| 107 | + return TC_FAIL; |
| 108 | + } |
| 109 | + data = 0x0; |
| 110 | + if (i2c_reg_read_byte(i2c_dev, ADV7513_HDMI_I2C_SLAVE_ADDR, |
| 111 | + ADV7513_WRITE_TEST_REG, &data)) { |
| 112 | + TC_PRINT("i2c read fail\n"); |
| 113 | + return TC_FAIL; |
| 114 | + } |
| 115 | + if (data != WRITE_TEST_VAL) { |
| 116 | + TC_PRINT("i2c write test failed 0x%x\n", data); |
| 117 | + return TC_FAIL; |
| 118 | + } |
| 119 | + TC_PRINT("i2c write & verify test passed\n"); |
| 120 | + |
| 121 | + return TC_PASS; |
| 122 | +} |
| 123 | + |
| 124 | +void test_i2c_master(void) |
| 125 | +{ |
| 126 | + zassert_true(test_i2c_adv7513() == TC_PASS, NULL); |
| 127 | +} |
| 128 | + |
| 129 | +void test_main(void) |
| 130 | +{ |
| 131 | + ztest_test_suite(nios2_i2c_master_test, |
| 132 | + ztest_unit_test(test_i2c_master)); |
| 133 | + ztest_run_test_suite(nios2_i2c_master_test); |
| 134 | +} |
0 commit comments