| .. | .. |
|---|
| 1 | +#include <linux/module.h> |
|---|
| 2 | +#include <linux/clk-provider.h> |
|---|
| 3 | +#include <linux/i2c.h> |
|---|
| 4 | +#include <linux/delay.h> |
|---|
| 5 | + |
|---|
| 6 | +static struct i2c_client s_m_client; |
|---|
| 7 | +static struct i2c_client* m_client; |
|---|
| 8 | + |
|---|
| 9 | +//写16位寄存器 |
|---|
| 10 | +static inline int nkmcu_reg_write(struct i2c_client *client,int index,unsigned short reg, unsigned char val) |
|---|
| 11 | +{ |
|---|
| 12 | + unsigned char u8_buf[3] = { 0 }; |
|---|
| 13 | + unsigned int buf_len = 3; |
|---|
| 14 | + int retry, timeout = 5; |
|---|
| 15 | + int ret; |
|---|
| 16 | + |
|---|
| 17 | + ret = 0; |
|---|
| 18 | + u8_buf[0] = (reg >> 8) & 0xFF;//寄存器地址高位 |
|---|
| 19 | + u8_buf[1] = reg & 0xFF; //寄存器地址低位 |
|---|
| 20 | + u8_buf[2] = val; //要发送的数据 |
|---|
| 21 | + |
|---|
| 22 | + //pdata->client->addr = ADDR + index; |
|---|
| 23 | + for (retry = 0; retry < timeout; retry++) |
|---|
| 24 | + { |
|---|
| 25 | + if (i2c_master_send(client, u8_buf, buf_len) < 0) |
|---|
| 26 | + { |
|---|
| 27 | + pr_err("%s:write reg error: reg=0x%x, val=0x%x, retry = %d.\n", __func__, reg, val, retry); |
|---|
| 28 | + ret = -1; |
|---|
| 29 | + msleep(5); |
|---|
| 30 | + continue; |
|---|
| 31 | + } |
|---|
| 32 | + else |
|---|
| 33 | + { |
|---|
| 34 | + pr_err("%s:write reg ok: reg=0x%x, val=0x%x, retry = %d.\n", __func__, reg, val, retry); |
|---|
| 35 | + ret = 0; |
|---|
| 36 | + break; |
|---|
| 37 | + } |
|---|
| 38 | + } |
|---|
| 39 | + return ret; |
|---|
| 40 | +} |
|---|
| 41 | + |
|---|
| 42 | +//16位读 |
|---|
| 43 | +static inline int nkmcu_reg_read(struct i2c_client *client, int index, unsigned short reg) |
|---|
| 44 | +{ |
|---|
| 45 | + unsigned char u8_buf[2] = { 0 }; |
|---|
| 46 | + unsigned int buf_len = 2; |
|---|
| 47 | + int retry, timeout = 5; |
|---|
| 48 | + unsigned char u8_val = 0; |
|---|
| 49 | + |
|---|
| 50 | + u8_buf[0] = (reg >> 8) & 0xFF;//寄存器地址高位 |
|---|
| 51 | + u8_buf[1] = reg & 0xFF;//寄存器地址低位 |
|---|
| 52 | + |
|---|
| 53 | + //pdata->client->addr = ADDR + index; |
|---|
| 54 | + for (retry = 0; retry < timeout; retry++) |
|---|
| 55 | + { |
|---|
| 56 | + if (i2c_master_send(client, u8_buf, buf_len) < 0) |
|---|
| 57 | + { |
|---|
| 58 | + pr_err("%s:read reg error on send: reg=0x%x, retry = %d.\n", __func__, reg, retry); |
|---|
| 59 | + msleep(5); |
|---|
| 60 | + continue; |
|---|
| 61 | + } |
|---|
| 62 | + if (i2c_master_recv(client, &u8_val, 1) != 1) { |
|---|
| 63 | + pr_err("%s:read reg error on recv: reg=0x%x, retry = %d.\n", __func__, reg, retry); |
|---|
| 64 | + msleep(5); |
|---|
| 65 | + continue; |
|---|
| 66 | + } |
|---|
| 67 | + break; |
|---|
| 68 | + } |
|---|
| 69 | + |
|---|
| 70 | + if (retry >= timeout) { |
|---|
| 71 | + pr_err("%s:read reg error: reg=0x%x.\n", __func__, reg); |
|---|
| 72 | + return -1; |
|---|
| 73 | + } |
|---|
| 74 | + |
|---|
| 75 | + return u8_val; |
|---|
| 76 | +} |
|---|
| 77 | + |
|---|
| 78 | +void nkmcu_device_shutdown(void) |
|---|
| 79 | +{ |
|---|
| 80 | + int ret; |
|---|
| 81 | + |
|---|
| 82 | + pr_err("nkmcu_device_shutdown.. \n"); |
|---|
| 83 | + |
|---|
| 84 | + //powenoff |
|---|
| 85 | + if (m_client != NULL) |
|---|
| 86 | + { |
|---|
| 87 | + ret = nkmcu_reg_write(m_client, 0x00, 0x06, 0x01); |
|---|
| 88 | + if (ret < 0) |
|---|
| 89 | + pr_err("nkmcu_device_shutdown..failed \n"); |
|---|
| 90 | + else |
|---|
| 91 | + pr_err("nkmcu_device_shutdown.. ok \n"); |
|---|
| 92 | + } |
|---|
| 93 | + |
|---|
| 94 | +} |
|---|
| 95 | +EXPORT_SYMBOL(nkmcu_device_shutdown); |
|---|
| 96 | + |
|---|
| 97 | +static int nk_mcu_probe(struct i2c_client *client, const struct i2c_device_id *id) |
|---|
| 98 | +{ |
|---|
| 99 | + //struct device_node *np = client->dev.of_node; |
|---|
| 100 | + int ret; |
|---|
| 101 | + |
|---|
| 102 | + printk("%s: probe\n", __FUNCTION__); |
|---|
| 103 | + |
|---|
| 104 | + /* check state of calendar information */ |
|---|
| 105 | + //device id |
|---|
| 106 | + //i2c_smbus_read_word_data |
|---|
| 107 | + msleep(100); |
|---|
| 108 | + //ret = i2c_smbus_read_byte_data(client, 0x00); |
|---|
| 109 | + ret = nkmcu_reg_read(client, 0x00, 0x00); |
|---|
| 110 | + printk("device id:%x \n", ret); |
|---|
| 111 | + if (ret != 0xaa) |
|---|
| 112 | + goto error; |
|---|
| 113 | + |
|---|
| 114 | + //powenon |
|---|
| 115 | + msleep(100); |
|---|
| 116 | + //ret = i2c_smbus_write_word_data(client, 0x07, 0x01); |
|---|
| 117 | + ret = nkmcu_reg_write(client, 0x00, 0x07, 0x01); |
|---|
| 118 | + printk("poweron command:%x \n", ret); |
|---|
| 119 | + if (ret < 0) |
|---|
| 120 | + goto error; |
|---|
| 121 | + |
|---|
| 122 | + //m_client = client; |
|---|
| 123 | + memcpy(&s_m_client, client, sizeof(struct i2c_client)); |
|---|
| 124 | + m_client = &s_m_client; |
|---|
| 125 | + return 0; |
|---|
| 126 | + |
|---|
| 127 | +error: |
|---|
| 128 | + return -1; |
|---|
| 129 | +} |
|---|
| 130 | + |
|---|
| 131 | +static const struct i2c_device_id nk_mcu_id[] = { |
|---|
| 132 | + { "nk_mcu", 0 }, |
|---|
| 133 | + { } |
|---|
| 134 | +}; |
|---|
| 135 | + |
|---|
| 136 | +static struct i2c_driver nk_mcu_driver = { |
|---|
| 137 | + .driver = { |
|---|
| 138 | + .name = "nk_mcu", |
|---|
| 139 | + .owner = THIS_MODULE, |
|---|
| 140 | + }, |
|---|
| 141 | + .probe = nk_mcu_probe, |
|---|
| 142 | + .id_table = nk_mcu_id, |
|---|
| 143 | +}; |
|---|
| 144 | + |
|---|
| 145 | +static int __init nk_mcu_init(void) |
|---|
| 146 | +{ |
|---|
| 147 | + return i2c_add_driver(&nk_mcu_driver); |
|---|
| 148 | +} |
|---|
| 149 | + |
|---|
| 150 | +static void __exit nk_mcu_exit(void) |
|---|
| 151 | +{ |
|---|
| 152 | + i2c_del_driver(&nk_mcu_driver); |
|---|
| 153 | +} |
|---|
| 154 | + |
|---|
| 155 | + |
|---|
| 156 | +MODULE_AUTHOR("ben@hotmail.com"); |
|---|
| 157 | +MODULE_DESCRIPTION("nodka mcu driver"); |
|---|
| 158 | +MODULE_LICENSE("GPL"); |
|---|
| 159 | + |
|---|
| 160 | +late_initcall(nk_mcu_init); |
|---|
| 161 | +module_exit(nk_mcu_exit); |
|---|