|
| 1 | +/* |
| 2 | + * |
| 3 | + * Tinker board Touchscreen MCU driver. |
| 4 | + * |
| 5 | + * Copyright (c) 2016 ASUSTek Computer Inc. |
| 6 | + * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved. |
| 7 | + * |
| 8 | + * This software is licensed under the terms of the GNU General Public |
| 9 | + * License version 2, as published by the Free Software Foundation, and |
| 10 | + * may be copied, distributed, and modified under those terms. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +#include <linux/module.h> |
| 20 | +#include <linux/slab.h> |
| 21 | +#include <linux/delay.h> |
| 22 | +#include <linux/i2c.h> |
| 23 | +#include <linux/module.h> |
| 24 | +#include <linux/workqueue.h> |
| 25 | +#include "tinker_mcu.h" |
| 26 | + |
| 27 | +static struct tinker_mcu_data *g_mcu_data; |
| 28 | +static int connected = 0; |
| 29 | + |
| 30 | +static int is_hex(char num) |
| 31 | +{ |
| 32 | + //0-9, a-f, A-F |
| 33 | + if ((47 < num && num < 58) || (64 < num && num < 71) || (96 < num && num < 103)) |
| 34 | + return 1; |
| 35 | + return 0; |
| 36 | +} |
| 37 | + |
| 38 | +static int string_to_byte(const char *source, unsigned char *destination, int size) |
| 39 | +{ |
| 40 | + int i = 0, counter = 0; |
| 41 | + char c[3] = {0}; |
| 42 | + unsigned char bytes; |
| 43 | + |
| 44 | + if (size%2 == 1) |
| 45 | + return -EINVAL; |
| 46 | + |
| 47 | + for(i = 0; i < size; i++){ |
| 48 | + if(!is_hex(source[i])) { |
| 49 | + return -EINVAL; |
| 50 | + } |
| 51 | + if(0 == i%2){ |
| 52 | + c[0] = source[i]; |
| 53 | + c[1] = source[i+1]; |
| 54 | + sscanf(c, "%hhx", &bytes); |
| 55 | + destination[counter] = bytes; |
| 56 | + counter++; |
| 57 | + } |
| 58 | + } |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +static int send_cmds(struct i2c_client *client, const char *buf) |
| 63 | +{ |
| 64 | + int ret, size = strlen(buf); |
| 65 | + unsigned char byte_cmd[size/2]; |
| 66 | + |
| 67 | + if ((size%2) != 0) { |
| 68 | + LOG_ERR("size should be even\n"); |
| 69 | + return -EINVAL; |
| 70 | + } |
| 71 | + |
| 72 | + LOG_INFO("%s\n", buf); |
| 73 | + |
| 74 | + string_to_byte(buf, byte_cmd, size); |
| 75 | + |
| 76 | + ret = i2c_master_send(client, byte_cmd, size/2); |
| 77 | + if (ret <= 0) { |
| 78 | + LOG_ERR("send command failed, ret = %d\n", ret); |
| 79 | + return ret!=0 ? ret : -ECOMM; |
| 80 | + } |
| 81 | + msleep(20); |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static int recv_cmds(struct i2c_client *client, char *buf, int size) |
| 86 | +{ |
| 87 | + int ret; |
| 88 | + |
| 89 | + ret = i2c_master_recv(client, buf, size); |
| 90 | + if (ret <= 0) { |
| 91 | + LOG_ERR("receive commands failed, %d\n", ret); |
| 92 | + return ret!=0 ? ret : -ECOMM; |
| 93 | + } |
| 94 | + msleep(20); |
| 95 | + return 0; |
| 96 | +} |
| 97 | + |
| 98 | +static int init_cmd_check(struct tinker_mcu_data *mcu_data) |
| 99 | +{ |
| 100 | + int ret; |
| 101 | + char recv_buf[1] = {0}; |
| 102 | + |
| 103 | + ret = send_cmds(mcu_data->client, "80"); |
| 104 | + if (ret < 0) |
| 105 | + goto error; |
| 106 | + |
| 107 | + recv_cmds(mcu_data->client, recv_buf, 1); |
| 108 | + if (ret < 0) |
| 109 | + goto error; |
| 110 | + |
| 111 | + LOG_INFO("recv_cmds: 0x%X\n", recv_buf[0]); |
| 112 | + if (recv_buf[0] != 0xC3) { |
| 113 | + LOG_ERR("read wrong info\n"); |
| 114 | + ret = -EINVAL; |
| 115 | + goto error; |
| 116 | + |
| 117 | + } |
| 118 | + return 0; |
| 119 | + |
| 120 | +error: |
| 121 | + return ret; |
| 122 | +} |
| 123 | + |
| 124 | +int tinker_mcu_screen_power_up(void) |
| 125 | +{ |
| 126 | + if (!connected) |
| 127 | + return -ENODEV; |
| 128 | + |
| 129 | + LOG_INFO("\n"); |
| 130 | + send_cmds(g_mcu_data->client, "8500"); |
| 131 | + msleep(800); |
| 132 | + send_cmds(g_mcu_data->client, "8501"); |
| 133 | + send_cmds(g_mcu_data->client, "8104"); |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
| 137 | +EXPORT_SYMBOL_GPL(tinker_mcu_screen_power_up); |
| 138 | + |
| 139 | +int tinker_mcu_set_bright(int bright) |
| 140 | +{ |
| 141 | + unsigned char cmd[2]; |
| 142 | + int ret; |
| 143 | + |
| 144 | + if (!connected) |
| 145 | + return -ENODEV; |
| 146 | + |
| 147 | + if (bright > 0xff || bright < 0) |
| 148 | + return -EINVAL; |
| 149 | + |
| 150 | + LOG_INFO("bright = 0x%x\n", bright); |
| 151 | + |
| 152 | + cmd[0] = 0x86; |
| 153 | + cmd[1] = bright; |
| 154 | + |
| 155 | + ret = i2c_master_send(g_mcu_data->client, cmd, 2); |
| 156 | + if (ret <= 0) { |
| 157 | + LOG_ERR("send command failed, ret = %d\n", ret); |
| 158 | + return ret != 0 ? ret : -ECOMM; |
| 159 | + } |
| 160 | + |
| 161 | + return 0; |
| 162 | +} |
| 163 | +EXPORT_SYMBOL_GPL(tinker_mcu_set_bright); |
| 164 | + |
| 165 | +int tinker_mcu_is_connected(void) |
| 166 | +{ |
| 167 | + return connected; |
| 168 | +} |
| 169 | +EXPORT_SYMBOL_GPL(tinker_mcu_is_connected); |
| 170 | + |
| 171 | +static int tinker_mcu_probe(struct i2c_client *client, |
| 172 | + const struct i2c_device_id *id) |
| 173 | +{ |
| 174 | + struct tinker_mcu_data *mcu_data; |
| 175 | + int ret; |
| 176 | + |
| 177 | + LOG_INFO("address = 0x%x\n", client->addr); |
| 178 | + |
| 179 | + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 180 | + LOG_ERR("I2C check functionality failed\n"); |
| 181 | + return -ENODEV; |
| 182 | + } |
| 183 | + |
| 184 | + mcu_data = kzalloc(sizeof(struct tinker_mcu_data), GFP_KERNEL); |
| 185 | + if (mcu_data == NULL) { |
| 186 | + LOG_ERR("no memory for device\n"); |
| 187 | + return -ENOMEM; |
| 188 | + } |
| 189 | + |
| 190 | + mcu_data->client = client; |
| 191 | + i2c_set_clientdata(client, mcu_data); |
| 192 | + g_mcu_data = mcu_data; |
| 193 | + |
| 194 | + ret = init_cmd_check(mcu_data); |
| 195 | + if (ret < 0) { |
| 196 | + LOG_ERR("init_cmd_check failed, %d\n", ret); |
| 197 | + goto error; |
| 198 | + } |
| 199 | + connected = 1; |
| 200 | + |
| 201 | + return 0; |
| 202 | + |
| 203 | +error: |
| 204 | + kfree(mcu_data); |
| 205 | + return ret; |
| 206 | +} |
| 207 | + |
| 208 | +static int tinker_mcu_remove(struct i2c_client *client) |
| 209 | +{ |
| 210 | + struct tinker_mcu_data *mcu_data = i2c_get_clientdata(client); |
| 211 | + connected = 0; |
| 212 | + kfree(mcu_data); |
| 213 | + return 0; |
| 214 | +} |
| 215 | + |
| 216 | +static const struct i2c_device_id tinker_mcu_id[] = { |
| 217 | + {"tinker_mcu", 0}, |
| 218 | + {}, |
| 219 | +}; |
| 220 | + |
| 221 | +static struct i2c_driver tinker_mcu_driver = { |
| 222 | + .driver = { |
| 223 | + .name = "tinker_mcu", |
| 224 | + }, |
| 225 | + .probe = tinker_mcu_probe, |
| 226 | + .remove = tinker_mcu_remove, |
| 227 | + .id_table = tinker_mcu_id, |
| 228 | +}; |
| 229 | +module_i2c_driver(tinker_mcu_driver); |
| 230 | + |
| 231 | +MODULE_DESCRIPTION("Tinker Board TouchScreen MCU driver"); |
| 232 | +MODULE_LICENSE("GPL v2"); |
0 commit comments