Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Shared Transport Line discipline driver Core |
| 3 | * Init Manager module responsible for GPIO control |
| 4 | * and firmware download |
Pavan Savoy | a0cc2f3 | 2010-10-06 12:18:14 -0400 | [diff] [blame] | 5 | * Copyright (C) 2009-2010 Texas Instruments |
| 6 | * Author: Pavan Savoy <pavan_savoy@ti.com> |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 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 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #define pr_fmt(fmt) "(stk) :" fmt |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/firmware.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/wait.h> |
| 29 | #include <linux/gpio.h> |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/seq_file.h> |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 32 | #include <linux/sched.h> |
Randy Dunlap | 773d679 | 2011-04-26 09:18:51 -0700 | [diff] [blame] | 33 | #include <linux/sysfs.h> |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 34 | #include <linux/tty.h> |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 35 | |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 36 | #include <linux/skbuff.h> |
Pavan Savoy | e555867 | 2010-09-30 16:13:30 -0400 | [diff] [blame] | 37 | #include <linux/ti_wilink_st.h> |
Paul Gortmaker | eb12a67 | 2011-07-03 15:14:56 -0400 | [diff] [blame] | 38 | #include <linux/module.h> |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 39 | |
Pavan Savoy | dbd3a87 | 2010-08-19 14:08:51 -0400 | [diff] [blame] | 40 | #define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */ |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 41 | static struct platform_device *st_kim_devices[MAX_ST_DEVICES]; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 42 | |
| 43 | /**********************************************************************/ |
| 44 | /* internal functions */ |
| 45 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 46 | /** |
Pavan Savoy | dbd3a87 | 2010-08-19 14:08:51 -0400 | [diff] [blame] | 47 | * st_get_plat_device - |
| 48 | * function which returns the reference to the platform device |
| 49 | * requested by id. As of now only 1 such device exists (id=0) |
| 50 | * the context requesting for reference can get the id to be |
| 51 | * requested by a. The protocol driver which is registering or |
| 52 | * b. the tty device which is opened. |
| 53 | */ |
| 54 | static struct platform_device *st_get_plat_device(int id) |
| 55 | { |
| 56 | return st_kim_devices[id]; |
| 57 | } |
| 58 | |
| 59 | /** |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 60 | * validate_firmware_response - |
| 61 | * function to return whether the firmware response was proper |
| 62 | * in case of error don't complete so that waiting for proper |
| 63 | * response times out |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 64 | */ |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 65 | static void validate_firmware_response(struct kim_data_s *kim_gdata) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 66 | { |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 67 | struct sk_buff *skb = kim_gdata->rx_skb; |
Pavan Savoy | 8565adb | 2012-08-03 14:49:43 -0500 | [diff] [blame] | 68 | if (!skb) |
| 69 | return; |
| 70 | |
| 71 | /* these magic numbers are the position in the response buffer which |
| 72 | * allows us to distinguish whether the response is for the read |
| 73 | * version info. command |
| 74 | */ |
| 75 | if (skb->data[2] == 0x01 && skb->data[3] == 0x01 && |
| 76 | skb->data[4] == 0x10 && skb->data[5] == 0x00) { |
| 77 | /* fw version response */ |
| 78 | memcpy(kim_gdata->resp_buffer, |
| 79 | kim_gdata->rx_skb->data, |
| 80 | kim_gdata->rx_skb->len); |
Pavan Savoy | 8565adb | 2012-08-03 14:49:43 -0500 | [diff] [blame] | 81 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; |
| 82 | kim_gdata->rx_skb = NULL; |
| 83 | kim_gdata->rx_count = 0; |
| 84 | } else if (unlikely(skb->data[5] != 0)) { |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 85 | pr_err("no proper response during fw download"); |
| 86 | pr_err("data6 %x", skb->data[5]); |
Pavan Savoy | 76ff0e6 | 2011-08-10 10:18:36 -0500 | [diff] [blame] | 87 | kfree_skb(skb); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 88 | return; /* keep waiting for the proper response */ |
| 89 | } |
| 90 | /* becos of all the script being downloaded */ |
| 91 | complete_all(&kim_gdata->kim_rcvd); |
| 92 | kfree_skb(skb); |
| 93 | } |
| 94 | |
| 95 | /* check for data len received inside kim_int_recv |
| 96 | * most often hit the last case to update state to waiting for data |
| 97 | */ |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 98 | static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 99 | { |
| 100 | register int room = skb_tailroom(kim_gdata->rx_skb); |
| 101 | |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 102 | pr_debug("len %d room %d", len, room); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 103 | |
| 104 | if (!len) { |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 105 | validate_firmware_response(kim_gdata); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 106 | } else if (len > room) { |
| 107 | /* Received packet's payload length is larger. |
| 108 | * We can't accommodate it in created skb. |
| 109 | */ |
| 110 | pr_err("Data length is too large len %d room %d", len, |
| 111 | room); |
| 112 | kfree_skb(kim_gdata->rx_skb); |
| 113 | } else { |
| 114 | /* Packet header has non-zero payload length and |
| 115 | * we have enough space in created skb. Lets read |
| 116 | * payload data */ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 117 | kim_gdata->rx_state = ST_W4_DATA; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 118 | kim_gdata->rx_count = len; |
| 119 | return len; |
| 120 | } |
| 121 | |
| 122 | /* Change ST LL state to continue to process next |
| 123 | * packet */ |
| 124 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; |
| 125 | kim_gdata->rx_skb = NULL; |
| 126 | kim_gdata->rx_count = 0; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 131 | /** |
| 132 | * kim_int_recv - receive function called during firmware download |
| 133 | * firmware download responses on different UART drivers |
| 134 | * have been observed to come in bursts of different |
| 135 | * tty_receive and hence the logic |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 136 | */ |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 137 | static void kim_int_recv(struct kim_data_s *kim_gdata, |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 138 | const unsigned char *data, long count) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 139 | { |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 140 | const unsigned char *ptr; |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 141 | int len = 0, type = 0; |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 142 | unsigned char *plen; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 143 | |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 144 | pr_debug("%s", __func__); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 145 | /* Decode received bytes here */ |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 146 | ptr = data; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 147 | if (unlikely(ptr == NULL)) { |
| 148 | pr_err(" received null from TTY "); |
| 149 | return; |
| 150 | } |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 151 | |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 152 | while (count) { |
| 153 | if (kim_gdata->rx_count) { |
| 154 | len = min_t(unsigned int, kim_gdata->rx_count, count); |
| 155 | memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len); |
| 156 | kim_gdata->rx_count -= len; |
| 157 | count -= len; |
| 158 | ptr += len; |
| 159 | |
| 160 | if (kim_gdata->rx_count) |
| 161 | continue; |
| 162 | |
| 163 | /* Check ST RX state machine , where are we? */ |
| 164 | switch (kim_gdata->rx_state) { |
| 165 | /* Waiting for complete packet ? */ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 166 | case ST_W4_DATA: |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 167 | pr_debug("Complete pkt received"); |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 168 | validate_firmware_response(kim_gdata); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 169 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; |
| 170 | kim_gdata->rx_skb = NULL; |
| 171 | continue; |
| 172 | /* Waiting for Bluetooth event header ? */ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 173 | case ST_W4_HEADER: |
| 174 | plen = |
| 175 | (unsigned char *)&kim_gdata->rx_skb->data[1]; |
| 176 | pr_debug("event hdr: plen 0x%02x\n", *plen); |
| 177 | kim_check_data_len(kim_gdata, *plen); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 178 | continue; |
| 179 | } /* end of switch */ |
| 180 | } /* end of if rx_state */ |
| 181 | switch (*ptr) { |
| 182 | /* Bluetooth event packet? */ |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 183 | case 0x04: |
| 184 | kim_gdata->rx_state = ST_W4_HEADER; |
| 185 | kim_gdata->rx_count = 2; |
| 186 | type = *ptr; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 187 | break; |
| 188 | default: |
| 189 | pr_info("unknown packet"); |
| 190 | ptr++; |
| 191 | count--; |
| 192 | continue; |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 193 | } |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 194 | ptr++; |
| 195 | count--; |
| 196 | kim_gdata->rx_skb = |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 197 | alloc_skb(1024+8, GFP_ATOMIC); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 198 | if (!kim_gdata->rx_skb) { |
| 199 | pr_err("can't allocate mem for new packet"); |
| 200 | kim_gdata->rx_state = ST_W4_PACKET_TYPE; |
| 201 | kim_gdata->rx_count = 0; |
| 202 | return; |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 203 | } |
Pavan Savoy | 5c88b02 | 2011-02-04 02:23:09 -0600 | [diff] [blame] | 204 | skb_reserve(kim_gdata->rx_skb, 8); |
| 205 | kim_gdata->rx_skb->cb[0] = 4; |
| 206 | kim_gdata->rx_skb->cb[1] = 0; |
| 207 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 208 | } |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 212 | static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 213 | { |
| 214 | unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0; |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 215 | const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 }; |
Nicholas Mc Guire | 3f46d81 | 2015-01-20 06:27:45 +0100 | [diff] [blame] | 216 | long timeout; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 217 | |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 218 | pr_debug("%s", __func__); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 219 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 220 | reinit_completion(&kim_gdata->kim_rcvd); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 221 | if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) { |
| 222 | pr_err("kim: couldn't write 4 bytes"); |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 223 | return -EIO; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Nicholas Mc Guire | 3f46d81 | 2015-01-20 06:27:45 +0100 | [diff] [blame] | 226 | timeout = wait_for_completion_interruptible_timeout( |
| 227 | &kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME)); |
| 228 | if (timeout <= 0) { |
| 229 | pr_err(" waiting for ver info- timed out or received signal"); |
| 230 | return timeout ? -ERESTARTSYS : -ETIMEDOUT; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 231 | } |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 232 | reinit_completion(&kim_gdata->kim_rcvd); |
Pavan Savoy | 8565adb | 2012-08-03 14:49:43 -0500 | [diff] [blame] | 233 | /* the positions 12 & 13 in the response buffer provide with the |
| 234 | * chip, major & minor numbers |
| 235 | */ |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 236 | |
| 237 | version = |
Pavan Savoy | 8565adb | 2012-08-03 14:49:43 -0500 | [diff] [blame] | 238 | MAKEWORD(kim_gdata->resp_buffer[12], |
| 239 | kim_gdata->resp_buffer[13]); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 240 | chip = (version & 0x7C00) >> 10; |
| 241 | min_ver = (version & 0x007F); |
| 242 | maj_ver = (version & 0x0380) >> 7; |
| 243 | |
| 244 | if (version & 0x8000) |
| 245 | maj_ver |= 0x0008; |
| 246 | |
Enric Balletbo i Serra | 4dcc2ab | 2014-07-22 13:08:38 +0200 | [diff] [blame] | 247 | sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts", |
| 248 | chip, maj_ver, min_ver); |
Naveen Jain | e2a5328 | 2010-06-09 03:45:33 -0500 | [diff] [blame] | 249 | |
| 250 | /* to be accessed later via sysfs entry */ |
| 251 | kim_gdata->version.full = version; |
| 252 | kim_gdata->version.chip = chip; |
| 253 | kim_gdata->version.maj_ver = maj_ver; |
| 254 | kim_gdata->version.min_ver = min_ver; |
| 255 | |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 256 | pr_info("%s", bts_scr_name); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 257 | return 0; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 258 | } |
| 259 | |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 260 | static void skip_change_remote_baud(unsigned char **ptr, long *len) |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 261 | { |
| 262 | unsigned char *nxt_action, *cur_action; |
| 263 | cur_action = *ptr; |
| 264 | |
| 265 | nxt_action = cur_action + sizeof(struct bts_action) + |
| 266 | ((struct bts_action *) cur_action)->size; |
| 267 | |
| 268 | if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) { |
| 269 | pr_err("invalid action after change remote baud command"); |
| 270 | } else { |
| 271 | *ptr = *ptr + sizeof(struct bts_action) + |
Shahar Lev | 9d031d9 | 2011-05-23 11:36:11 +0300 | [diff] [blame] | 272 | ((struct bts_action *)cur_action)->size; |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 273 | *len = *len - (sizeof(struct bts_action) + |
Shahar Lev | 9d031d9 | 2011-05-23 11:36:11 +0300 | [diff] [blame] | 274 | ((struct bts_action *)cur_action)->size); |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 275 | /* warn user on not commenting these in firmware */ |
| 276 | pr_warn("skipping the wait event of change remote baud"); |
| 277 | } |
| 278 | } |
| 279 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 280 | /** |
| 281 | * download_firmware - |
| 282 | * internal function which parses through the .bts firmware |
| 283 | * script file intreprets SEND, DELAY actions only as of now |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 284 | */ |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 285 | static long download_firmware(struct kim_data_s *kim_gdata) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 286 | { |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 287 | long err = 0; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 288 | long len = 0; |
Pavan Savoy | 73f12e8 | 2010-10-12 16:27:38 -0400 | [diff] [blame] | 289 | unsigned char *ptr = NULL; |
| 290 | unsigned char *action_ptr = NULL; |
Enric Balletbo i Serra | 4dcc2ab | 2014-07-22 13:08:38 +0200 | [diff] [blame] | 291 | unsigned char bts_scr_name[40] = { 0 }; /* 40 char long bts scr name? */ |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 292 | int wr_room_space; |
| 293 | int cmd_size; |
| 294 | unsigned long timeout; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 295 | |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 296 | err = read_local_version(kim_gdata, bts_scr_name); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 297 | if (err != 0) { |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 298 | pr_err("kim: failed to read local ver"); |
| 299 | return err; |
| 300 | } |
| 301 | err = |
| 302 | request_firmware(&kim_gdata->fw_entry, bts_scr_name, |
| 303 | &kim_gdata->kim_pdev->dev); |
| 304 | if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) || |
| 305 | (kim_gdata->fw_entry->size == 0))) { |
| 306 | pr_err(" request_firmware failed(errno %ld) for %s", err, |
| 307 | bts_scr_name); |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 308 | return -EINVAL; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 309 | } |
| 310 | ptr = (void *)kim_gdata->fw_entry->data; |
| 311 | len = kim_gdata->fw_entry->size; |
| 312 | /* bts_header to remove out magic number and |
| 313 | * version |
| 314 | */ |
| 315 | ptr += sizeof(struct bts_header); |
| 316 | len -= sizeof(struct bts_header); |
| 317 | |
| 318 | while (len > 0 && ptr) { |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 319 | pr_debug(" action size %d, type %d ", |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 320 | ((struct bts_action *)ptr)->size, |
| 321 | ((struct bts_action *)ptr)->type); |
| 322 | |
| 323 | switch (((struct bts_action *)ptr)->type) { |
| 324 | case ACTION_SEND_COMMAND: /* action send */ |
Pavan Savoy | 2f81a02 | 2011-08-10 10:18:34 -0500 | [diff] [blame] | 325 | pr_debug("S"); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 326 | action_ptr = &(((struct bts_action *)ptr)->data[0]); |
| 327 | if (unlikely |
| 328 | (((struct hci_command *)action_ptr)->opcode == |
| 329 | 0xFF36)) { |
| 330 | /* ignore remote change |
| 331 | * baud rate HCI VS command */ |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 332 | pr_warn("change remote baud" |
Pavan Savoy | e6d9e64 | 2010-07-22 05:32:05 -0500 | [diff] [blame] | 333 | " rate command in firmware"); |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 334 | skip_change_remote_baud(&ptr, &len); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 335 | break; |
| 336 | } |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 337 | /* |
| 338 | * Make sure we have enough free space in uart |
| 339 | * tx buffer to write current firmware command |
| 340 | */ |
| 341 | cmd_size = ((struct bts_action *)ptr)->size; |
| 342 | timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME); |
| 343 | do { |
| 344 | wr_room_space = |
| 345 | st_get_uart_wr_room(kim_gdata->core_data); |
| 346 | if (wr_room_space < 0) { |
| 347 | pr_err("Unable to get free " |
| 348 | "space info from uart tx buffer"); |
| 349 | release_firmware(kim_gdata->fw_entry); |
| 350 | return wr_room_space; |
| 351 | } |
| 352 | mdelay(1); /* wait 1ms before checking room */ |
| 353 | } while ((wr_room_space < cmd_size) && |
| 354 | time_before(jiffies, timeout)); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 355 | |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 356 | /* Timeout happened ? */ |
| 357 | if (time_after_eq(jiffies, timeout)) { |
| 358 | pr_err("Timeout while waiting for free " |
| 359 | "free space in uart tx buffer"); |
| 360 | release_firmware(kim_gdata->fw_entry); |
| 361 | return -ETIMEDOUT; |
| 362 | } |
Pavan Savoy | 2f81a02 | 2011-08-10 10:18:34 -0500 | [diff] [blame] | 363 | /* reinit completion before sending for the |
| 364 | * relevant wait |
| 365 | */ |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 366 | reinit_completion(&kim_gdata->kim_rcvd); |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * Free space found in uart buffer, call st_int_write |
| 370 | * to send current firmware command to the uart tx |
| 371 | * buffer. |
| 372 | */ |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 373 | err = st_int_write(kim_gdata->core_data, |
| 374 | ((struct bts_action_send *)action_ptr)->data, |
| 375 | ((struct bts_action *)ptr)->size); |
| 376 | if (unlikely(err < 0)) { |
| 377 | release_firmware(kim_gdata->fw_entry); |
Pavan Savoy | 7044266 | 2011-02-04 02:23:11 -0600 | [diff] [blame] | 378 | return err; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 379 | } |
Pavan Savoy | ef04d12 | 2011-02-04 02:23:13 -0600 | [diff] [blame] | 380 | /* |
| 381 | * Check number of bytes written to the uart tx buffer |
| 382 | * and requested command write size |
| 383 | */ |
| 384 | if (err != cmd_size) { |
| 385 | pr_err("Number of bytes written to uart " |
| 386 | "tx buffer are not matching with " |
| 387 | "requested cmd write size"); |
| 388 | release_firmware(kim_gdata->fw_entry); |
| 389 | return -EIO; |
| 390 | } |
| 391 | break; |
| 392 | case ACTION_WAIT_EVENT: /* wait */ |
Pavan Savoy | 2f81a02 | 2011-08-10 10:18:34 -0500 | [diff] [blame] | 393 | pr_debug("W"); |
Nicholas Mc Guire | 3f46d81 | 2015-01-20 06:27:45 +0100 | [diff] [blame] | 394 | err = wait_for_completion_interruptible_timeout( |
Pavan Savoy | 5370235 | 2012-08-03 14:49:42 -0500 | [diff] [blame] | 395 | &kim_gdata->kim_rcvd, |
Nicholas Mc Guire | 3f46d81 | 2015-01-20 06:27:45 +0100 | [diff] [blame] | 396 | msecs_to_jiffies(CMD_RESP_TIME)); |
| 397 | if (err <= 0) { |
| 398 | pr_err("response timeout/signaled during fw download "); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 399 | /* timed out */ |
| 400 | release_firmware(kim_gdata->fw_entry); |
Nicholas Mc Guire | 3f46d81 | 2015-01-20 06:27:45 +0100 | [diff] [blame] | 401 | return err ? -ERESTARTSYS : -ETIMEDOUT; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 402 | } |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 403 | reinit_completion(&kim_gdata->kim_rcvd); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 404 | break; |
| 405 | case ACTION_DELAY: /* sleep */ |
| 406 | pr_info("sleep command in scr"); |
| 407 | action_ptr = &(((struct bts_action *)ptr)->data[0]); |
| 408 | mdelay(((struct bts_action_delay *)action_ptr)->msec); |
| 409 | break; |
| 410 | } |
| 411 | len = |
| 412 | len - (sizeof(struct bts_action) + |
| 413 | ((struct bts_action *)ptr)->size); |
| 414 | ptr = |
| 415 | ptr + sizeof(struct bts_action) + |
| 416 | ((struct bts_action *)ptr)->size; |
| 417 | } |
| 418 | /* fw download complete */ |
| 419 | release_firmware(kim_gdata->fw_entry); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 420 | return 0; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /**********************************************************************/ |
| 424 | /* functions called from ST core */ |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 425 | /* called from ST Core, when REG_IN_PROGRESS (registration in progress) |
| 426 | * can be because of |
| 427 | * 1. response to read local version |
| 428 | * 2. during send/recv's of firmware download |
| 429 | */ |
| 430 | void st_kim_recv(void *disc_data, const unsigned char *data, long count) |
| 431 | { |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 432 | struct st_data_s *st_gdata = (struct st_data_s *)disc_data; |
| 433 | struct kim_data_s *kim_gdata = st_gdata->kim_data; |
| 434 | |
Pavan Savoy | 8565adb | 2012-08-03 14:49:43 -0500 | [diff] [blame] | 435 | /* proceed to gather all data and distinguish read fw version response |
| 436 | * from other fw responses when data gathering is complete |
| 437 | */ |
| 438 | kim_int_recv(kim_gdata, data, count); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | |
| 442 | /* to signal completion of line discipline installation |
| 443 | * called from ST Core, upon tty_open |
| 444 | */ |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 445 | void st_kim_complete(void *kim_data) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 446 | { |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 447 | struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 448 | complete(&kim_gdata->ldisc_installed); |
| 449 | } |
| 450 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 451 | /** |
| 452 | * st_kim_start - called from ST Core upon 1st registration |
| 453 | * This involves toggling the chip enable gpio, reading |
| 454 | * the firmware version from chip, forming the fw file name |
| 455 | * based on the chip version, requesting the fw, parsing it |
| 456 | * and perform download(send/recv). |
| 457 | */ |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 458 | long st_kim_start(void *kim_data) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 459 | { |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 460 | long err = 0; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 461 | long retry = POR_RETRY_COUNT; |
Pavan Savoy | 0d7c5f2 | 2011-08-10 10:18:31 -0500 | [diff] [blame] | 462 | struct ti_st_plat_data *pdata; |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 463 | struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; |
| 464 | |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 465 | pr_info(" %s", __func__); |
Rob Herring | c0bd1b9 | 2015-07-22 13:17:15 -0500 | [diff] [blame] | 466 | pdata = kim_gdata->kim_pdev->dev.platform_data; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 467 | |
| 468 | do { |
Pavan Savoy | 0d7c5f2 | 2011-08-10 10:18:31 -0500 | [diff] [blame] | 469 | /* platform specific enabling code here */ |
| 470 | if (pdata->chip_enable) |
| 471 | pdata->chip_enable(kim_gdata); |
| 472 | |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 473 | /* Configure BT nShutdown to HIGH state */ |
Jürg Billeter | 513da46 | 2015-06-24 12:24:06 +0200 | [diff] [blame] | 474 | gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW); |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 475 | mdelay(5); /* FIXME: a proper toggle */ |
Jürg Billeter | 513da46 | 2015-06-24 12:24:06 +0200 | [diff] [blame] | 476 | gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH); |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 477 | mdelay(100); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 478 | /* re-initialize the completion */ |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 479 | reinit_completion(&kim_gdata->ldisc_installed); |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 480 | /* send notification to UIM */ |
| 481 | kim_gdata->ldisc_install = 1; |
| 482 | pr_info("ldisc_install = 1"); |
| 483 | sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, |
| 484 | NULL, "install"); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 485 | /* wait for ldisc to be installed */ |
Pavan Savoy | 5370235 | 2012-08-03 14:49:42 -0500 | [diff] [blame] | 486 | err = wait_for_completion_interruptible_timeout( |
| 487 | &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME)); |
Pavan Savoy | 18ccecf | 2011-12-15 10:38:21 -0600 | [diff] [blame] | 488 | if (!err) { |
| 489 | /* ldisc installation timeout, |
| 490 | * flush uart, power cycle BT_EN */ |
| 491 | pr_err("ldisc installation timeout"); |
| 492 | err = st_kim_stop(kim_gdata); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 493 | continue; |
| 494 | } else { |
| 495 | /* ldisc installed now */ |
Pavan Savoy | 18ccecf | 2011-12-15 10:38:21 -0600 | [diff] [blame] | 496 | pr_info("line discipline installed"); |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 497 | err = download_firmware(kim_gdata); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 498 | if (err != 0) { |
Pavan Savoy | 18ccecf | 2011-12-15 10:38:21 -0600 | [diff] [blame] | 499 | /* ldisc installed but fw download failed, |
| 500 | * flush uart & power cycle BT_EN */ |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 501 | pr_err("download firmware failed"); |
Pavan Savoy | 18ccecf | 2011-12-15 10:38:21 -0600 | [diff] [blame] | 502 | err = st_kim_stop(kim_gdata); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 503 | continue; |
| 504 | } else { /* on success don't retry */ |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | } while (retry--); |
| 509 | return err; |
| 510 | } |
| 511 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 512 | /** |
Pavan Savoy | 18ccecf | 2011-12-15 10:38:21 -0600 | [diff] [blame] | 513 | * st_kim_stop - stop communication with chip. |
| 514 | * This can be called from ST Core/KIM, on the- |
| 515 | * (a) last un-register when chip need not be powered there-after, |
| 516 | * (b) upon failure to either install ldisc or download firmware. |
| 517 | * The function is responsible to (a) notify UIM about un-installation, |
| 518 | * (b) flush UART if the ldisc was installed. |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 519 | * (c) reset BT_EN - pull down nshutdown at the end. |
| 520 | * (d) invoke platform's chip disabling routine. |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 521 | */ |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 522 | long st_kim_stop(void *kim_data) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 523 | { |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 524 | long err = 0; |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 525 | struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; |
Rob Herring | c0bd1b9 | 2015-07-22 13:17:15 -0500 | [diff] [blame] | 526 | struct ti_st_plat_data *pdata = |
| 527 | kim_gdata->kim_pdev->dev.platform_data; |
Pavan Savoy | 18ccecf | 2011-12-15 10:38:21 -0600 | [diff] [blame] | 528 | struct tty_struct *tty = kim_gdata->core_data->tty; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 529 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 530 | reinit_completion(&kim_gdata->ldisc_installed); |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 531 | |
Pavan Savoy | 18ccecf | 2011-12-15 10:38:21 -0600 | [diff] [blame] | 532 | if (tty) { /* can be called before ldisc is installed */ |
| 533 | /* Flush any pending characters in the driver and discipline. */ |
| 534 | tty_ldisc_flush(tty); |
| 535 | tty_driver_flush_buffer(tty); |
Pavan Savoy | 18ccecf | 2011-12-15 10:38:21 -0600 | [diff] [blame] | 536 | } |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 537 | |
| 538 | /* send uninstall notification to UIM */ |
| 539 | pr_info("ldisc_install = 0"); |
| 540 | kim_gdata->ldisc_install = 0; |
| 541 | sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install"); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 542 | |
| 543 | /* wait for ldisc to be un-installed */ |
Pavan Savoy | 5370235 | 2012-08-03 14:49:42 -0500 | [diff] [blame] | 544 | err = wait_for_completion_interruptible_timeout( |
| 545 | &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME)); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 546 | if (!err) { /* timeout */ |
| 547 | pr_err(" timed out waiting for ldisc to be un-installed"); |
Pavan Savoy | b64365a | 2012-08-03 14:49:41 -0500 | [diff] [blame] | 548 | err = -ETIMEDOUT; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 549 | } |
| 550 | |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 551 | /* By default configure BT nShutdown to LOW state */ |
Jürg Billeter | 513da46 | 2015-06-24 12:24:06 +0200 | [diff] [blame] | 552 | gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW); |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 553 | mdelay(1); |
Jürg Billeter | 513da46 | 2015-06-24 12:24:06 +0200 | [diff] [blame] | 554 | gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH); |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 555 | mdelay(1); |
Jürg Billeter | 513da46 | 2015-06-24 12:24:06 +0200 | [diff] [blame] | 556 | gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW); |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 557 | |
Pavan Savoy | 0d7c5f2 | 2011-08-10 10:18:31 -0500 | [diff] [blame] | 558 | /* platform specific disable */ |
| 559 | if (pdata->chip_disable) |
| 560 | pdata->chip_disable(kim_gdata); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 561 | return err; |
| 562 | } |
| 563 | |
| 564 | /**********************************************************************/ |
| 565 | /* functions called from subsystems */ |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 566 | /* called when debugfs entry is read from */ |
Naveen Jain | e2a5328 | 2010-06-09 03:45:33 -0500 | [diff] [blame] | 567 | |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 568 | static int show_version(struct seq_file *s, void *unused) |
Naveen Jain | e2a5328 | 2010-06-09 03:45:33 -0500 | [diff] [blame] | 569 | { |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 570 | struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private; |
| 571 | seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full, |
Naveen Jain | e2a5328 | 2010-06-09 03:45:33 -0500 | [diff] [blame] | 572 | kim_gdata->version.chip, kim_gdata->version.maj_ver, |
| 573 | kim_gdata->version.min_ver); |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 574 | return 0; |
Naveen Jain | e2a5328 | 2010-06-09 03:45:33 -0500 | [diff] [blame] | 575 | } |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 576 | |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 577 | static int show_list(struct seq_file *s, void *unused) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 578 | { |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 579 | struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private; |
| 580 | kim_st_list_protocols(kim_gdata->core_data, s); |
| 581 | return 0; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 582 | } |
| 583 | |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 584 | static ssize_t show_install(struct device *dev, |
| 585 | struct device_attribute *attr, char *buf) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 586 | { |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 587 | struct kim_data_s *kim_data = dev_get_drvdata(dev); |
| 588 | return sprintf(buf, "%d\n", kim_data->ldisc_install); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 589 | } |
| 590 | |
Pavan Savoy | 933aae5 | 2011-12-15 10:38:22 -0600 | [diff] [blame] | 591 | #ifdef DEBUG |
| 592 | static ssize_t store_dev_name(struct device *dev, |
| 593 | struct device_attribute *attr, const char *buf, size_t count) |
| 594 | { |
| 595 | struct kim_data_s *kim_data = dev_get_drvdata(dev); |
| 596 | pr_debug("storing dev name >%s<", buf); |
| 597 | strncpy(kim_data->dev_name, buf, count); |
| 598 | pr_debug("stored dev name >%s<", kim_data->dev_name); |
| 599 | return count; |
| 600 | } |
| 601 | |
| 602 | static ssize_t store_baud_rate(struct device *dev, |
| 603 | struct device_attribute *attr, const char *buf, size_t count) |
| 604 | { |
| 605 | struct kim_data_s *kim_data = dev_get_drvdata(dev); |
| 606 | pr_debug("storing baud rate >%s<", buf); |
| 607 | sscanf(buf, "%ld", &kim_data->baud_rate); |
| 608 | pr_debug("stored baud rate >%ld<", kim_data->baud_rate); |
| 609 | return count; |
| 610 | } |
| 611 | #endif /* if DEBUG */ |
| 612 | |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 613 | static ssize_t show_dev_name(struct device *dev, |
| 614 | struct device_attribute *attr, char *buf) |
| 615 | { |
| 616 | struct kim_data_s *kim_data = dev_get_drvdata(dev); |
| 617 | return sprintf(buf, "%s\n", kim_data->dev_name); |
| 618 | } |
| 619 | |
| 620 | static ssize_t show_baud_rate(struct device *dev, |
| 621 | struct device_attribute *attr, char *buf) |
| 622 | { |
| 623 | struct kim_data_s *kim_data = dev_get_drvdata(dev); |
Dan Carpenter | 63e144c | 2015-01-15 14:42:27 +0300 | [diff] [blame] | 624 | return sprintf(buf, "%d\n", kim_data->baud_rate); |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static ssize_t show_flow_cntrl(struct device *dev, |
| 628 | struct device_attribute *attr, char *buf) |
| 629 | { |
| 630 | struct kim_data_s *kim_data = dev_get_drvdata(dev); |
| 631 | return sprintf(buf, "%d\n", kim_data->flow_cntrl); |
| 632 | } |
| 633 | |
| 634 | /* structures specific for sysfs entries */ |
| 635 | static struct kobj_attribute ldisc_install = |
| 636 | __ATTR(install, 0444, (void *)show_install, NULL); |
| 637 | |
| 638 | static struct kobj_attribute uart_dev_name = |
Pavan Savoy | 933aae5 | 2011-12-15 10:38:22 -0600 | [diff] [blame] | 639 | #ifdef DEBUG /* TODO: move this to debug-fs if possible */ |
| 640 | __ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name); |
| 641 | #else |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 642 | __ATTR(dev_name, 0444, (void *)show_dev_name, NULL); |
Pavan Savoy | 933aae5 | 2011-12-15 10:38:22 -0600 | [diff] [blame] | 643 | #endif |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 644 | |
| 645 | static struct kobj_attribute uart_baud_rate = |
Pavan Savoy | 933aae5 | 2011-12-15 10:38:22 -0600 | [diff] [blame] | 646 | #ifdef DEBUG /* TODO: move to debugfs */ |
| 647 | __ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate); |
| 648 | #else |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 649 | __ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL); |
Pavan Savoy | 933aae5 | 2011-12-15 10:38:22 -0600 | [diff] [blame] | 650 | #endif |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 651 | |
| 652 | static struct kobj_attribute uart_flow_cntrl = |
| 653 | __ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL); |
| 654 | |
| 655 | static struct attribute *uim_attrs[] = { |
| 656 | &ldisc_install.attr, |
| 657 | &uart_dev_name.attr, |
| 658 | &uart_baud_rate.attr, |
| 659 | &uart_flow_cntrl.attr, |
| 660 | NULL, |
| 661 | }; |
| 662 | |
| 663 | static struct attribute_group uim_attr_grp = { |
| 664 | .attrs = uim_attrs, |
| 665 | }; |
| 666 | |
Pavan Savoy | 36b5aee | 2010-07-22 05:32:06 -0500 | [diff] [blame] | 667 | /** |
| 668 | * st_kim_ref - reference the core's data |
| 669 | * This references the per-ST platform device in the arch/xx/ |
| 670 | * board-xx.c file. |
| 671 | * This would enable multiple such platform devices to exist |
| 672 | * on a given platform |
| 673 | */ |
Pavan Savoy | dbd3a87 | 2010-08-19 14:08:51 -0400 | [diff] [blame] | 674 | void st_kim_ref(struct st_data_s **core_data, int id) |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 675 | { |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 676 | struct platform_device *pdev; |
| 677 | struct kim_data_s *kim_gdata; |
| 678 | /* get kim_gdata reference from platform device */ |
Pavan Savoy | dbd3a87 | 2010-08-19 14:08:51 -0400 | [diff] [blame] | 679 | pdev = st_get_plat_device(id); |
Gigi Joseph | 868eba8 | 2015-01-09 03:48:29 +0000 | [diff] [blame] | 680 | if (!pdev) |
| 681 | goto err; |
Jingoo Han | 9093ca8 | 2013-05-23 19:35:23 +0900 | [diff] [blame] | 682 | kim_gdata = platform_get_drvdata(pdev); |
Gigi Joseph | 868eba8 | 2015-01-09 03:48:29 +0000 | [diff] [blame] | 683 | if (!kim_gdata) |
| 684 | goto err; |
| 685 | |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 686 | *core_data = kim_gdata->core_data; |
Gigi Joseph | 868eba8 | 2015-01-09 03:48:29 +0000 | [diff] [blame] | 687 | return; |
| 688 | err: |
| 689 | *core_data = NULL; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 690 | } |
| 691 | |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 692 | static int kim_version_open(struct inode *i, struct file *f) |
| 693 | { |
| 694 | return single_open(f, show_version, i->i_private); |
| 695 | } |
| 696 | |
| 697 | static int kim_list_open(struct inode *i, struct file *f) |
| 698 | { |
| 699 | return single_open(f, show_list, i->i_private); |
| 700 | } |
| 701 | |
| 702 | static const struct file_operations version_debugfs_fops = { |
| 703 | /* version info */ |
| 704 | .open = kim_version_open, |
| 705 | .read = seq_read, |
| 706 | .llseek = seq_lseek, |
| 707 | .release = single_release, |
| 708 | }; |
| 709 | static const struct file_operations list_debugfs_fops = { |
| 710 | /* protocols info */ |
| 711 | .open = kim_list_open, |
| 712 | .read = seq_read, |
| 713 | .llseek = seq_lseek, |
| 714 | .release = single_release, |
| 715 | }; |
| 716 | |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 717 | /**********************************************************************/ |
| 718 | /* functions called from platform device driver subsystem |
| 719 | * need to have a relevant platform device entry in the platform's |
| 720 | * board-*.c file |
| 721 | */ |
| 722 | |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 723 | static struct dentry *kim_debugfs_dir; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 724 | static int kim_probe(struct platform_device *pdev) |
| 725 | { |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 726 | struct kim_data_s *kim_gdata; |
Rob Herring | c0bd1b9 | 2015-07-22 13:17:15 -0500 | [diff] [blame] | 727 | struct ti_st_plat_data *pdata = pdev->dev.platform_data; |
Matthias Kaehlcke | b299738 | 2012-11-22 23:26:14 +0100 | [diff] [blame] | 728 | int err; |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 729 | |
Pavan Savoy | dfb7ef7 | 2010-09-10 15:58:55 -0400 | [diff] [blame] | 730 | if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) { |
| 731 | /* multiple devices could exist */ |
| 732 | st_kim_devices[pdev->id] = pdev; |
| 733 | } else { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 734 | /* platform's sure about existence of 1 device */ |
Pavan Savoy | dfb7ef7 | 2010-09-10 15:58:55 -0400 | [diff] [blame] | 735 | st_kim_devices[0] = pdev; |
| 736 | } |
| 737 | |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 738 | kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC); |
| 739 | if (!kim_gdata) { |
| 740 | pr_err("no mem to allocate"); |
| 741 | return -ENOMEM; |
| 742 | } |
Jingoo Han | 9093ca8 | 2013-05-23 19:35:23 +0900 | [diff] [blame] | 743 | platform_set_drvdata(pdev, kim_gdata); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 744 | |
Matthias Kaehlcke | b299738 | 2012-11-22 23:26:14 +0100 | [diff] [blame] | 745 | err = st_core_init(&kim_gdata->core_data); |
| 746 | if (err != 0) { |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 747 | pr_err(" ST core init failed"); |
Matthias Kaehlcke | b299738 | 2012-11-22 23:26:14 +0100 | [diff] [blame] | 748 | err = -EIO; |
| 749 | goto err_core_init; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 750 | } |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 751 | /* refer to itself */ |
| 752 | kim_gdata->core_data->kim_data = kim_gdata; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 753 | |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 754 | /* Claim the chip enable nShutdown gpio from the system */ |
| 755 | kim_gdata->nshutdown = pdata->nshutdown_gpio; |
| 756 | err = gpio_request(kim_gdata->nshutdown, "kim"); |
| 757 | if (unlikely(err)) { |
Dan Carpenter | 63e144c | 2015-01-15 14:42:27 +0300 | [diff] [blame] | 758 | pr_err(" gpio %d request failed ", kim_gdata->nshutdown); |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 759 | return err; |
| 760 | } |
| 761 | |
| 762 | /* Configure nShutdown GPIO as output=0 */ |
| 763 | err = gpio_direction_output(kim_gdata->nshutdown, 0); |
| 764 | if (unlikely(err)) { |
Dan Carpenter | 63e144c | 2015-01-15 14:42:27 +0300 | [diff] [blame] | 765 | pr_err(" unable to configure gpio %d", kim_gdata->nshutdown); |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 766 | return err; |
| 767 | } |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 768 | /* get reference of pdev for request_firmware |
| 769 | */ |
| 770 | kim_gdata->kim_pdev = pdev; |
| 771 | init_completion(&kim_gdata->kim_rcvd); |
| 772 | init_completion(&kim_gdata->ldisc_installed); |
Naveen Jain | b38fc2d | 2010-06-09 03:45:32 -0500 | [diff] [blame] | 773 | |
Matthias Kaehlcke | b299738 | 2012-11-22 23:26:14 +0100 | [diff] [blame] | 774 | err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp); |
| 775 | if (err) { |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 776 | pr_err("failed to create sysfs entries"); |
Matthias Kaehlcke | b299738 | 2012-11-22 23:26:14 +0100 | [diff] [blame] | 777 | goto err_sysfs_group; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 778 | } |
Naveen Jain | e2a5328 | 2010-06-09 03:45:33 -0500 | [diff] [blame] | 779 | |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 780 | /* copying platform data */ |
| 781 | strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN); |
| 782 | kim_gdata->flow_cntrl = pdata->flow_cntrl; |
| 783 | kim_gdata->baud_rate = pdata->baud_rate; |
| 784 | pr_info("sysfs entries created\n"); |
| 785 | |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 786 | kim_debugfs_dir = debugfs_create_dir("ti-st", NULL); |
Robin van der Gracht | 3768528 | 2014-06-16 16:38:56 +0200 | [diff] [blame] | 787 | if (!kim_debugfs_dir) { |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 788 | pr_err(" debugfs entries creation failed "); |
Gigi Joseph | c6ec0fb | 2015-01-09 03:47:51 +0000 | [diff] [blame] | 789 | return 0; |
Naveen Jain | e2a5328 | 2010-06-09 03:45:33 -0500 | [diff] [blame] | 790 | } |
Pavan Savoy | c1afac1 | 2010-07-28 02:25:59 -0500 | [diff] [blame] | 791 | |
| 792 | debugfs_create_file("version", S_IRUGO, kim_debugfs_dir, |
| 793 | kim_gdata, &version_debugfs_fops); |
| 794 | debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir, |
| 795 | kim_gdata, &list_debugfs_fops); |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 796 | return 0; |
Matthias Kaehlcke | b299738 | 2012-11-22 23:26:14 +0100 | [diff] [blame] | 797 | |
Matthias Kaehlcke | b299738 | 2012-11-22 23:26:14 +0100 | [diff] [blame] | 798 | err_sysfs_group: |
| 799 | st_core_exit(kim_gdata->core_data); |
| 800 | |
| 801 | err_core_init: |
| 802 | kfree(kim_gdata); |
| 803 | |
| 804 | return err; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static int kim_remove(struct platform_device *pdev) |
| 808 | { |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 809 | /* free the GPIOs requested */ |
Rob Herring | c0bd1b9 | 2015-07-22 13:17:15 -0500 | [diff] [blame] | 810 | struct ti_st_plat_data *pdata = pdev->dev.platform_data; |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 811 | struct kim_data_s *kim_gdata; |
| 812 | |
Jingoo Han | 9093ca8 | 2013-05-23 19:35:23 +0900 | [diff] [blame] | 813 | kim_gdata = platform_get_drvdata(pdev); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 814 | |
Luciano Coelho | a7e2ca1 | 2013-01-21 13:12:42 +0200 | [diff] [blame] | 815 | /* Free the Bluetooth/FM/GPIO |
| 816 | * nShutdown gpio from the system |
| 817 | */ |
| 818 | gpio_free(pdata->nshutdown_gpio); |
| 819 | pr_info("nshutdown GPIO Freed"); |
| 820 | |
Pavan Savoy | 781a739 | 2011-02-04 02:23:15 -0600 | [diff] [blame] | 821 | debugfs_remove_recursive(kim_debugfs_dir); |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 822 | sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp); |
Pavan Savoy | 781a739 | 2011-02-04 02:23:15 -0600 | [diff] [blame] | 823 | pr_info("sysfs entries removed"); |
| 824 | |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 825 | kim_gdata->kim_pdev = NULL; |
| 826 | st_core_exit(kim_gdata->core_data); |
Pavan Savoy | 38d9df4 | 2010-07-08 08:55:17 -0500 | [diff] [blame] | 827 | |
| 828 | kfree(kim_gdata); |
| 829 | kim_gdata = NULL; |
Pavan Savoy | 320920c | 2010-07-14 08:21:12 -0500 | [diff] [blame] | 830 | return 0; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 831 | } |
| 832 | |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 833 | static int kim_suspend(struct platform_device *pdev, pm_message_t state) |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 834 | { |
Rob Herring | c0bd1b9 | 2015-07-22 13:17:15 -0500 | [diff] [blame] | 835 | struct ti_st_plat_data *pdata = pdev->dev.platform_data; |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 836 | |
| 837 | if (pdata->suspend) |
| 838 | return pdata->suspend(pdev, state); |
| 839 | |
Gigi Joseph | 4b4aa3a | 2015-01-09 03:46:24 +0000 | [diff] [blame] | 840 | return 0; |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 841 | } |
| 842 | |
Pavan Savoy | 27712b3 | 2012-08-03 14:49:40 -0500 | [diff] [blame] | 843 | static int kim_resume(struct platform_device *pdev) |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 844 | { |
Rob Herring | c0bd1b9 | 2015-07-22 13:17:15 -0500 | [diff] [blame] | 845 | struct ti_st_plat_data *pdata = pdev->dev.platform_data; |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 846 | |
| 847 | if (pdata->resume) |
| 848 | return pdata->resume(pdev); |
| 849 | |
Gigi Joseph | 4b4aa3a | 2015-01-09 03:46:24 +0000 | [diff] [blame] | 850 | return 0; |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 851 | } |
| 852 | |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 853 | /**********************************************************************/ |
| 854 | /* entry point for ST KIM module, called in from ST Core */ |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 855 | static struct platform_driver kim_platform_driver = { |
| 856 | .probe = kim_probe, |
| 857 | .remove = kim_remove, |
| 858 | .suspend = kim_suspend, |
| 859 | .resume = kim_resume, |
| 860 | .driver = { |
| 861 | .name = "kim", |
Pavan Savoy | ec60d0a | 2011-02-04 02:23:10 -0600 | [diff] [blame] | 862 | }, |
| 863 | }; |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 864 | |
Axel Lin | b00e126 | 2012-01-22 15:33:49 +0800 | [diff] [blame] | 865 | module_platform_driver(kim_platform_driver); |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 866 | |
Pavan Savoy | d0088ce | 2010-04-08 13:16:54 -0500 | [diff] [blame] | 867 | MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>"); |
| 868 | MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips "); |
| 869 | MODULE_LICENSE("GPL"); |