blob: 5027b8ffae4378e0aeda58513f993778bb34c30c [file] [log] [blame]
Pavan Savoyd0088ce2010-04-08 13:16:54 -05001/*
2 * Shared Transport Line discipline driver Core
3 * Init Manager module responsible for GPIO control
4 * and firmware download
Pavan Savoya0cc2f32010-10-06 12:18:14 -04005 * Copyright (C) 2009-2010 Texas Instruments
6 * Author: Pavan Savoy <pavan_savoy@ti.com>
Pavan Savoyd0088ce2010-04-08 13:16:54 -05007 *
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 Savoyc1afac12010-07-28 02:25:59 -050030#include <linux/debugfs.h>
31#include <linux/seq_file.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050032#include <linux/sched.h>
Randy Dunlap773d6792011-04-26 09:18:51 -070033#include <linux/sysfs.h>
Pavan Savoyec60d0a2011-02-04 02:23:10 -060034#include <linux/tty.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050035
Pavan Savoy5c88b022011-02-04 02:23:09 -060036#include <linux/skbuff.h>
Pavan Savoye5558672010-09-30 16:13:30 -040037#include <linux/ti_wilink_st.h>
Paul Gortmakereb12a672011-07-03 15:14:56 -040038#include <linux/module.h>
Gigi Joseph46d0d332015-01-09 03:45:02 +000039#include <linux/of.h>
40#include <linux/of_device.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050041
Pavan Savoydbd3a872010-08-19 14:08:51 -040042#define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
Pavan Savoy73f12e82010-10-12 16:27:38 -040043static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
Pavan Savoyd0088ce2010-04-08 13:16:54 -050044
45/**********************************************************************/
46/* internal functions */
47
Gigi Joseph46d0d332015-01-09 03:45:02 +000048struct ti_st_plat_data *dt_pdata;
49static struct ti_st_plat_data *get_platform_data(struct device *dev);
50
Pavan Savoy36b5aee2010-07-22 05:32:06 -050051/**
Pavan Savoydbd3a872010-08-19 14:08:51 -040052 * st_get_plat_device -
53 * function which returns the reference to the platform device
54 * requested by id. As of now only 1 such device exists (id=0)
55 * the context requesting for reference can get the id to be
56 * requested by a. The protocol driver which is registering or
57 * b. the tty device which is opened.
58 */
59static struct platform_device *st_get_plat_device(int id)
60{
61 return st_kim_devices[id];
62}
63
64/**
Pavan Savoy36b5aee2010-07-22 05:32:06 -050065 * validate_firmware_response -
66 * function to return whether the firmware response was proper
67 * in case of error don't complete so that waiting for proper
68 * response times out
Pavan Savoyd0088ce2010-04-08 13:16:54 -050069 */
Pavan Savoy27712b32012-08-03 14:49:40 -050070static void validate_firmware_response(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -050071{
Pavan Savoy38d9df42010-07-08 08:55:17 -050072 struct sk_buff *skb = kim_gdata->rx_skb;
Pavan Savoy8565adb2012-08-03 14:49:43 -050073 if (!skb)
74 return;
75
76 /* these magic numbers are the position in the response buffer which
77 * allows us to distinguish whether the response is for the read
78 * version info. command
79 */
80 if (skb->data[2] == 0x01 && skb->data[3] == 0x01 &&
81 skb->data[4] == 0x10 && skb->data[5] == 0x00) {
82 /* fw version response */
83 memcpy(kim_gdata->resp_buffer,
84 kim_gdata->rx_skb->data,
85 kim_gdata->rx_skb->len);
86 complete_all(&kim_gdata->kim_rcvd);
87 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
88 kim_gdata->rx_skb = NULL;
89 kim_gdata->rx_count = 0;
90 } else if (unlikely(skb->data[5] != 0)) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -050091 pr_err("no proper response during fw download");
92 pr_err("data6 %x", skb->data[5]);
Pavan Savoy76ff0e62011-08-10 10:18:36 -050093 kfree_skb(skb);
Pavan Savoyd0088ce2010-04-08 13:16:54 -050094 return; /* keep waiting for the proper response */
95 }
96 /* becos of all the script being downloaded */
97 complete_all(&kim_gdata->kim_rcvd);
98 kfree_skb(skb);
99}
100
101/* check for data len received inside kim_int_recv
102 * most often hit the last case to update state to waiting for data
103 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500104static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500105{
106 register int room = skb_tailroom(kim_gdata->rx_skb);
107
Pavan Savoye6d9e642010-07-22 05:32:05 -0500108 pr_debug("len %d room %d", len, room);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500109
110 if (!len) {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500111 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500112 } else if (len > room) {
113 /* Received packet's payload length is larger.
114 * We can't accommodate it in created skb.
115 */
116 pr_err("Data length is too large len %d room %d", len,
117 room);
118 kfree_skb(kim_gdata->rx_skb);
119 } else {
120 /* Packet header has non-zero payload length and
121 * we have enough space in created skb. Lets read
122 * payload data */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600123 kim_gdata->rx_state = ST_W4_DATA;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500124 kim_gdata->rx_count = len;
125 return len;
126 }
127
128 /* Change ST LL state to continue to process next
129 * packet */
130 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
131 kim_gdata->rx_skb = NULL;
132 kim_gdata->rx_count = 0;
133
134 return 0;
135}
136
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500137/**
138 * kim_int_recv - receive function called during firmware download
139 * firmware download responses on different UART drivers
140 * have been observed to come in bursts of different
141 * tty_receive and hence the logic
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500142 */
Pavan Savoy27712b32012-08-03 14:49:40 -0500143static void kim_int_recv(struct kim_data_s *kim_gdata,
Pavan Savoy38d9df42010-07-08 08:55:17 -0500144 const unsigned char *data, long count)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500145{
Pavan Savoy73f12e82010-10-12 16:27:38 -0400146 const unsigned char *ptr;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400147 int len = 0, type = 0;
Pavan Savoy5c88b022011-02-04 02:23:09 -0600148 unsigned char *plen;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500149
Pavan Savoye6d9e642010-07-22 05:32:05 -0500150 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500151 /* Decode received bytes here */
Pavan Savoy73f12e82010-10-12 16:27:38 -0400152 ptr = data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500153 if (unlikely(ptr == NULL)) {
154 pr_err(" received null from TTY ");
155 return;
156 }
Pavan Savoy73f12e82010-10-12 16:27:38 -0400157
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500158 while (count) {
159 if (kim_gdata->rx_count) {
160 len = min_t(unsigned int, kim_gdata->rx_count, count);
161 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
162 kim_gdata->rx_count -= len;
163 count -= len;
164 ptr += len;
165
166 if (kim_gdata->rx_count)
167 continue;
168
169 /* Check ST RX state machine , where are we? */
170 switch (kim_gdata->rx_state) {
171 /* Waiting for complete packet ? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600172 case ST_W4_DATA:
Pavan Savoye6d9e642010-07-22 05:32:05 -0500173 pr_debug("Complete pkt received");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500174 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500175 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
176 kim_gdata->rx_skb = NULL;
177 continue;
178 /* Waiting for Bluetooth event header ? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600179 case ST_W4_HEADER:
180 plen =
181 (unsigned char *)&kim_gdata->rx_skb->data[1];
182 pr_debug("event hdr: plen 0x%02x\n", *plen);
183 kim_check_data_len(kim_gdata, *plen);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500184 continue;
185 } /* end of switch */
186 } /* end of if rx_state */
187 switch (*ptr) {
188 /* Bluetooth event packet? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600189 case 0x04:
190 kim_gdata->rx_state = ST_W4_HEADER;
191 kim_gdata->rx_count = 2;
192 type = *ptr;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500193 break;
194 default:
195 pr_info("unknown packet");
196 ptr++;
197 count--;
198 continue;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500199 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500200 ptr++;
201 count--;
202 kim_gdata->rx_skb =
Pavan Savoy5c88b022011-02-04 02:23:09 -0600203 alloc_skb(1024+8, GFP_ATOMIC);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500204 if (!kim_gdata->rx_skb) {
205 pr_err("can't allocate mem for new packet");
206 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
207 kim_gdata->rx_count = 0;
208 return;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500209 }
Pavan Savoy5c88b022011-02-04 02:23:09 -0600210 skb_reserve(kim_gdata->rx_skb, 8);
211 kim_gdata->rx_skb->cb[0] = 4;
212 kim_gdata->rx_skb->cb[1] = 0;
213
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500214 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500215 return;
216}
217
Pavan Savoy38d9df42010-07-08 08:55:17 -0500218static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500219{
220 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400221 const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
Nicholas Mc Guire3f46d812015-01-20 06:27:45 +0100222 long timeout;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500223
Pavan Savoye6d9e642010-07-22 05:32:05 -0500224 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500225
Wolfram Sang16735d02013-11-14 14:32:02 -0800226 reinit_completion(&kim_gdata->kim_rcvd);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500227 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
228 pr_err("kim: couldn't write 4 bytes");
Pavan Savoy70442662011-02-04 02:23:11 -0600229 return -EIO;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500230 }
231
Nicholas Mc Guire3f46d812015-01-20 06:27:45 +0100232 timeout = wait_for_completion_interruptible_timeout(
233 &kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME));
234 if (timeout <= 0) {
235 pr_err(" waiting for ver info- timed out or received signal");
236 return timeout ? -ERESTARTSYS : -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500237 }
Wolfram Sang16735d02013-11-14 14:32:02 -0800238 reinit_completion(&kim_gdata->kim_rcvd);
Pavan Savoy8565adb2012-08-03 14:49:43 -0500239 /* the positions 12 & 13 in the response buffer provide with the
240 * chip, major & minor numbers
241 */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500242
243 version =
Pavan Savoy8565adb2012-08-03 14:49:43 -0500244 MAKEWORD(kim_gdata->resp_buffer[12],
245 kim_gdata->resp_buffer[13]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500246 chip = (version & 0x7C00) >> 10;
247 min_ver = (version & 0x007F);
248 maj_ver = (version & 0x0380) >> 7;
249
250 if (version & 0x8000)
251 maj_ver |= 0x0008;
252
Enric Balletbo i Serra4dcc2ab2014-07-22 13:08:38 +0200253 sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts",
254 chip, maj_ver, min_ver);
Naveen Jaine2a53282010-06-09 03:45:33 -0500255
256 /* to be accessed later via sysfs entry */
257 kim_gdata->version.full = version;
258 kim_gdata->version.chip = chip;
259 kim_gdata->version.maj_ver = maj_ver;
260 kim_gdata->version.min_ver = min_ver;
261
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500262 pr_info("%s", bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500263 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500264}
265
Pavan Savoy27712b32012-08-03 14:49:40 -0500266static void skip_change_remote_baud(unsigned char **ptr, long *len)
Pavan Savoyef04d122011-02-04 02:23:13 -0600267{
268 unsigned char *nxt_action, *cur_action;
269 cur_action = *ptr;
270
271 nxt_action = cur_action + sizeof(struct bts_action) +
272 ((struct bts_action *) cur_action)->size;
273
274 if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
275 pr_err("invalid action after change remote baud command");
276 } else {
277 *ptr = *ptr + sizeof(struct bts_action) +
Shahar Lev9d031d92011-05-23 11:36:11 +0300278 ((struct bts_action *)cur_action)->size;
Pavan Savoyef04d122011-02-04 02:23:13 -0600279 *len = *len - (sizeof(struct bts_action) +
Shahar Lev9d031d92011-05-23 11:36:11 +0300280 ((struct bts_action *)cur_action)->size);
Pavan Savoyef04d122011-02-04 02:23:13 -0600281 /* warn user on not commenting these in firmware */
282 pr_warn("skipping the wait event of change remote baud");
283 }
284}
285
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500286/**
287 * download_firmware -
288 * internal function which parses through the .bts firmware
289 * script file intreprets SEND, DELAY actions only as of now
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500290 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500291static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500292{
Pavan Savoy320920c2010-07-14 08:21:12 -0500293 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500294 long len = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400295 unsigned char *ptr = NULL;
296 unsigned char *action_ptr = NULL;
Enric Balletbo i Serra4dcc2ab2014-07-22 13:08:38 +0200297 unsigned char bts_scr_name[40] = { 0 }; /* 40 char long bts scr name? */
Pavan Savoyef04d122011-02-04 02:23:13 -0600298 int wr_room_space;
299 int cmd_size;
300 unsigned long timeout;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500301
Pavan Savoy38d9df42010-07-08 08:55:17 -0500302 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500303 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500304 pr_err("kim: failed to read local ver");
305 return err;
306 }
307 err =
308 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
309 &kim_gdata->kim_pdev->dev);
310 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
311 (kim_gdata->fw_entry->size == 0))) {
312 pr_err(" request_firmware failed(errno %ld) for %s", err,
313 bts_scr_name);
Pavan Savoy70442662011-02-04 02:23:11 -0600314 return -EINVAL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500315 }
316 ptr = (void *)kim_gdata->fw_entry->data;
317 len = kim_gdata->fw_entry->size;
318 /* bts_header to remove out magic number and
319 * version
320 */
321 ptr += sizeof(struct bts_header);
322 len -= sizeof(struct bts_header);
323
324 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500325 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500326 ((struct bts_action *)ptr)->size,
327 ((struct bts_action *)ptr)->type);
328
329 switch (((struct bts_action *)ptr)->type) {
330 case ACTION_SEND_COMMAND: /* action send */
Pavan Savoy2f81a022011-08-10 10:18:34 -0500331 pr_debug("S");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500332 action_ptr = &(((struct bts_action *)ptr)->data[0]);
333 if (unlikely
334 (((struct hci_command *)action_ptr)->opcode ==
335 0xFF36)) {
336 /* ignore remote change
337 * baud rate HCI VS command */
Pavan Savoyef04d122011-02-04 02:23:13 -0600338 pr_warn("change remote baud"
Pavan Savoye6d9e642010-07-22 05:32:05 -0500339 " rate command in firmware");
Pavan Savoyef04d122011-02-04 02:23:13 -0600340 skip_change_remote_baud(&ptr, &len);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500341 break;
342 }
Pavan Savoyef04d122011-02-04 02:23:13 -0600343 /*
344 * Make sure we have enough free space in uart
345 * tx buffer to write current firmware command
346 */
347 cmd_size = ((struct bts_action *)ptr)->size;
348 timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
349 do {
350 wr_room_space =
351 st_get_uart_wr_room(kim_gdata->core_data);
352 if (wr_room_space < 0) {
353 pr_err("Unable to get free "
354 "space info from uart tx buffer");
355 release_firmware(kim_gdata->fw_entry);
356 return wr_room_space;
357 }
358 mdelay(1); /* wait 1ms before checking room */
359 } while ((wr_room_space < cmd_size) &&
360 time_before(jiffies, timeout));
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500361
Pavan Savoyef04d122011-02-04 02:23:13 -0600362 /* Timeout happened ? */
363 if (time_after_eq(jiffies, timeout)) {
364 pr_err("Timeout while waiting for free "
365 "free space in uart tx buffer");
366 release_firmware(kim_gdata->fw_entry);
367 return -ETIMEDOUT;
368 }
Pavan Savoy2f81a022011-08-10 10:18:34 -0500369 /* reinit completion before sending for the
370 * relevant wait
371 */
Wolfram Sang16735d02013-11-14 14:32:02 -0800372 reinit_completion(&kim_gdata->kim_rcvd);
Pavan Savoyef04d122011-02-04 02:23:13 -0600373
374 /*
375 * Free space found in uart buffer, call st_int_write
376 * to send current firmware command to the uart tx
377 * buffer.
378 */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500379 err = st_int_write(kim_gdata->core_data,
380 ((struct bts_action_send *)action_ptr)->data,
381 ((struct bts_action *)ptr)->size);
382 if (unlikely(err < 0)) {
383 release_firmware(kim_gdata->fw_entry);
Pavan Savoy70442662011-02-04 02:23:11 -0600384 return err;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500385 }
Pavan Savoyef04d122011-02-04 02:23:13 -0600386 /*
387 * Check number of bytes written to the uart tx buffer
388 * and requested command write size
389 */
390 if (err != cmd_size) {
391 pr_err("Number of bytes written to uart "
392 "tx buffer are not matching with "
393 "requested cmd write size");
394 release_firmware(kim_gdata->fw_entry);
395 return -EIO;
396 }
397 break;
398 case ACTION_WAIT_EVENT: /* wait */
Pavan Savoy2f81a022011-08-10 10:18:34 -0500399 pr_debug("W");
Nicholas Mc Guire3f46d812015-01-20 06:27:45 +0100400 err = wait_for_completion_interruptible_timeout(
Pavan Savoy53702352012-08-03 14:49:42 -0500401 &kim_gdata->kim_rcvd,
Nicholas Mc Guire3f46d812015-01-20 06:27:45 +0100402 msecs_to_jiffies(CMD_RESP_TIME));
403 if (err <= 0) {
404 pr_err("response timeout/signaled during fw download ");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500405 /* timed out */
406 release_firmware(kim_gdata->fw_entry);
Nicholas Mc Guire3f46d812015-01-20 06:27:45 +0100407 return err ? -ERESTARTSYS : -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500408 }
Wolfram Sang16735d02013-11-14 14:32:02 -0800409 reinit_completion(&kim_gdata->kim_rcvd);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500410 break;
411 case ACTION_DELAY: /* sleep */
412 pr_info("sleep command in scr");
413 action_ptr = &(((struct bts_action *)ptr)->data[0]);
414 mdelay(((struct bts_action_delay *)action_ptr)->msec);
415 break;
416 }
417 len =
418 len - (sizeof(struct bts_action) +
419 ((struct bts_action *)ptr)->size);
420 ptr =
421 ptr + sizeof(struct bts_action) +
422 ((struct bts_action *)ptr)->size;
423 }
424 /* fw download complete */
425 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500426 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500427}
428
429/**********************************************************************/
430/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500431/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
432 * can be because of
433 * 1. response to read local version
434 * 2. during send/recv's of firmware download
435 */
436void st_kim_recv(void *disc_data, const unsigned char *data, long count)
437{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500438 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
439 struct kim_data_s *kim_gdata = st_gdata->kim_data;
440
Pavan Savoy8565adb2012-08-03 14:49:43 -0500441 /* proceed to gather all data and distinguish read fw version response
442 * from other fw responses when data gathering is complete
443 */
444 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500445 return;
446}
447
448/* to signal completion of line discipline installation
449 * called from ST Core, upon tty_open
450 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500451void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500452{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500453 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500454 complete(&kim_gdata->ldisc_installed);
455}
456
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500457/**
458 * st_kim_start - called from ST Core upon 1st registration
459 * This involves toggling the chip enable gpio, reading
460 * the firmware version from chip, forming the fw file name
461 * based on the chip version, requesting the fw, parsing it
462 * and perform download(send/recv).
463 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500464long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500465{
Pavan Savoy320920c2010-07-14 08:21:12 -0500466 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500467 long retry = POR_RETRY_COUNT;
Pavan Savoy0d7c5f22011-08-10 10:18:31 -0500468 struct ti_st_plat_data *pdata;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500469 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
470
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500471 pr_info(" %s", __func__);
Gigi Joseph46d0d332015-01-09 03:45:02 +0000472 if (kim_gdata->kim_pdev->dev.of_node) {
473 pr_debug("use device tree data");
474 pdata = dt_pdata;
475 } else {
476 pdata = kim_gdata->kim_pdev->dev.platform_data;
477 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500478
479 do {
Pavan Savoy0d7c5f22011-08-10 10:18:31 -0500480 /* platform specific enabling code here */
481 if (pdata->chip_enable)
482 pdata->chip_enable(kim_gdata);
483
Luciano Coelhoa7e2ca12013-01-21 13:12:42 +0200484 /* Configure BT nShutdown to HIGH state */
485 gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
486 mdelay(5); /* FIXME: a proper toggle */
487 gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
488 mdelay(100);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500489 /* re-initialize the completion */
Wolfram Sang16735d02013-11-14 14:32:02 -0800490 reinit_completion(&kim_gdata->ldisc_installed);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600491 /* send notification to UIM */
492 kim_gdata->ldisc_install = 1;
493 pr_info("ldisc_install = 1");
494 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
495 NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500496 /* wait for ldisc to be installed */
Pavan Savoy53702352012-08-03 14:49:42 -0500497 err = wait_for_completion_interruptible_timeout(
498 &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600499 if (!err) {
500 /* ldisc installation timeout,
501 * flush uart, power cycle BT_EN */
502 pr_err("ldisc installation timeout");
503 err = st_kim_stop(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500504 continue;
505 } else {
506 /* ldisc installed now */
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600507 pr_info("line discipline installed");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500508 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500509 if (err != 0) {
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600510 /* ldisc installed but fw download failed,
511 * flush uart & power cycle BT_EN */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500512 pr_err("download firmware failed");
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600513 err = st_kim_stop(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500514 continue;
515 } else { /* on success don't retry */
516 break;
517 }
518 }
519 } while (retry--);
520 return err;
521}
522
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500523/**
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600524 * st_kim_stop - stop communication with chip.
525 * This can be called from ST Core/KIM, on the-
526 * (a) last un-register when chip need not be powered there-after,
527 * (b) upon failure to either install ldisc or download firmware.
528 * The function is responsible to (a) notify UIM about un-installation,
529 * (b) flush UART if the ldisc was installed.
Luciano Coelhoa7e2ca12013-01-21 13:12:42 +0200530 * (c) reset BT_EN - pull down nshutdown at the end.
531 * (d) invoke platform's chip disabling routine.
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500532 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500533long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500534{
Pavan Savoy320920c2010-07-14 08:21:12 -0500535 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500536 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Gigi Joseph46d0d332015-01-09 03:45:02 +0000537 struct ti_st_plat_data *pdata;
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600538 struct tty_struct *tty = kim_gdata->core_data->tty;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500539
Wolfram Sang16735d02013-11-14 14:32:02 -0800540 reinit_completion(&kim_gdata->ldisc_installed);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600541
Gigi Joseph46d0d332015-01-09 03:45:02 +0000542 if (kim_gdata->kim_pdev->dev.of_node) {
543 pr_debug("use device tree data");
544 pdata = dt_pdata;
545 } else
546 pdata = kim_gdata->kim_pdev->dev.platform_data;
547
548
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600549 if (tty) { /* can be called before ldisc is installed */
550 /* Flush any pending characters in the driver and discipline. */
551 tty_ldisc_flush(tty);
552 tty_driver_flush_buffer(tty);
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600553 }
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600554
555 /* send uninstall notification to UIM */
556 pr_info("ldisc_install = 0");
557 kim_gdata->ldisc_install = 0;
558 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500559
560 /* wait for ldisc to be un-installed */
Pavan Savoy53702352012-08-03 14:49:42 -0500561 err = wait_for_completion_interruptible_timeout(
562 &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500563 if (!err) { /* timeout */
564 pr_err(" timed out waiting for ldisc to be un-installed");
Pavan Savoyb64365a2012-08-03 14:49:41 -0500565 err = -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500566 }
567
Luciano Coelhoa7e2ca12013-01-21 13:12:42 +0200568 /* By default configure BT nShutdown to LOW state */
569 gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
570 mdelay(1);
571 gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
572 mdelay(1);
573 gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
574
Pavan Savoy0d7c5f22011-08-10 10:18:31 -0500575 /* platform specific disable */
576 if (pdata->chip_disable)
577 pdata->chip_disable(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500578 return err;
579}
580
581/**********************************************************************/
582/* functions called from subsystems */
Pavan Savoyc1afac12010-07-28 02:25:59 -0500583/* called when debugfs entry is read from */
Naveen Jaine2a53282010-06-09 03:45:33 -0500584
Pavan Savoyc1afac12010-07-28 02:25:59 -0500585static int show_version(struct seq_file *s, void *unused)
Naveen Jaine2a53282010-06-09 03:45:33 -0500586{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500587 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
588 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
Naveen Jaine2a53282010-06-09 03:45:33 -0500589 kim_gdata->version.chip, kim_gdata->version.maj_ver,
590 kim_gdata->version.min_ver);
Pavan Savoyc1afac12010-07-28 02:25:59 -0500591 return 0;
Naveen Jaine2a53282010-06-09 03:45:33 -0500592}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500593
Pavan Savoyc1afac12010-07-28 02:25:59 -0500594static int show_list(struct seq_file *s, void *unused)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500595{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500596 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
597 kim_st_list_protocols(kim_gdata->core_data, s);
598 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500599}
600
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600601static ssize_t show_install(struct device *dev,
602 struct device_attribute *attr, char *buf)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500603{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600604 struct kim_data_s *kim_data = dev_get_drvdata(dev);
605 return sprintf(buf, "%d\n", kim_data->ldisc_install);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500606}
607
Pavan Savoy933aae52011-12-15 10:38:22 -0600608#ifdef DEBUG
609static ssize_t store_dev_name(struct device *dev,
610 struct device_attribute *attr, const char *buf, size_t count)
611{
612 struct kim_data_s *kim_data = dev_get_drvdata(dev);
613 pr_debug("storing dev name >%s<", buf);
614 strncpy(kim_data->dev_name, buf, count);
615 pr_debug("stored dev name >%s<", kim_data->dev_name);
616 return count;
617}
618
619static ssize_t store_baud_rate(struct device *dev,
620 struct device_attribute *attr, const char *buf, size_t count)
621{
622 struct kim_data_s *kim_data = dev_get_drvdata(dev);
623 pr_debug("storing baud rate >%s<", buf);
624 sscanf(buf, "%ld", &kim_data->baud_rate);
625 pr_debug("stored baud rate >%ld<", kim_data->baud_rate);
626 return count;
627}
628#endif /* if DEBUG */
629
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600630static ssize_t show_dev_name(struct device *dev,
631 struct device_attribute *attr, char *buf)
632{
633 struct kim_data_s *kim_data = dev_get_drvdata(dev);
634 return sprintf(buf, "%s\n", kim_data->dev_name);
635}
636
637static ssize_t show_baud_rate(struct device *dev,
638 struct device_attribute *attr, char *buf)
639{
640 struct kim_data_s *kim_data = dev_get_drvdata(dev);
Dan Carpenter63e144c2015-01-15 14:42:27 +0300641 return sprintf(buf, "%d\n", kim_data->baud_rate);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600642}
643
644static ssize_t show_flow_cntrl(struct device *dev,
645 struct device_attribute *attr, char *buf)
646{
647 struct kim_data_s *kim_data = dev_get_drvdata(dev);
648 return sprintf(buf, "%d\n", kim_data->flow_cntrl);
649}
650
651/* structures specific for sysfs entries */
652static struct kobj_attribute ldisc_install =
653__ATTR(install, 0444, (void *)show_install, NULL);
654
655static struct kobj_attribute uart_dev_name =
Pavan Savoy933aae52011-12-15 10:38:22 -0600656#ifdef DEBUG /* TODO: move this to debug-fs if possible */
657__ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name);
658#else
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600659__ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
Pavan Savoy933aae52011-12-15 10:38:22 -0600660#endif
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600661
662static struct kobj_attribute uart_baud_rate =
Pavan Savoy933aae52011-12-15 10:38:22 -0600663#ifdef DEBUG /* TODO: move to debugfs */
664__ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate);
665#else
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600666__ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
Pavan Savoy933aae52011-12-15 10:38:22 -0600667#endif
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600668
669static struct kobj_attribute uart_flow_cntrl =
670__ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
671
672static struct attribute *uim_attrs[] = {
673 &ldisc_install.attr,
674 &uart_dev_name.attr,
675 &uart_baud_rate.attr,
676 &uart_flow_cntrl.attr,
677 NULL,
678};
679
680static struct attribute_group uim_attr_grp = {
681 .attrs = uim_attrs,
682};
683
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500684/**
685 * st_kim_ref - reference the core's data
686 * This references the per-ST platform device in the arch/xx/
687 * board-xx.c file.
688 * This would enable multiple such platform devices to exist
689 * on a given platform
690 */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400691void st_kim_ref(struct st_data_s **core_data, int id)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500692{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500693 struct platform_device *pdev;
694 struct kim_data_s *kim_gdata;
695 /* get kim_gdata reference from platform device */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400696 pdev = st_get_plat_device(id);
Gigi Joseph868eba82015-01-09 03:48:29 +0000697 if (!pdev)
698 goto err;
Jingoo Han9093ca82013-05-23 19:35:23 +0900699 kim_gdata = platform_get_drvdata(pdev);
Gigi Joseph868eba82015-01-09 03:48:29 +0000700 if (!kim_gdata)
701 goto err;
702
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500703 *core_data = kim_gdata->core_data;
Gigi Joseph868eba82015-01-09 03:48:29 +0000704 return;
705err:
706 *core_data = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500707}
708
Pavan Savoyc1afac12010-07-28 02:25:59 -0500709static int kim_version_open(struct inode *i, struct file *f)
710{
711 return single_open(f, show_version, i->i_private);
712}
713
714static int kim_list_open(struct inode *i, struct file *f)
715{
716 return single_open(f, show_list, i->i_private);
717}
718
719static const struct file_operations version_debugfs_fops = {
720 /* version info */
721 .open = kim_version_open,
722 .read = seq_read,
723 .llseek = seq_lseek,
724 .release = single_release,
725};
726static const struct file_operations list_debugfs_fops = {
727 /* protocols info */
728 .open = kim_list_open,
729 .read = seq_read,
730 .llseek = seq_lseek,
731 .release = single_release,
732};
733
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500734/**********************************************************************/
735/* functions called from platform device driver subsystem
736 * need to have a relevant platform device entry in the platform's
737 * board-*.c file
738 */
739
Gigi Joseph46d0d332015-01-09 03:45:02 +0000740static const struct of_device_id kim_of_match[] = {
741{
742 .compatible = "kim",
743 },
744 {}
745};
746MODULE_DEVICE_TABLE(of, kim_of_match);
747
748static struct ti_st_plat_data *get_platform_data(struct device *dev)
749{
750 struct device_node *np = dev->of_node;
751 const u32 *dt_property;
752 int len;
753
754 dt_pdata = kzalloc(sizeof(*dt_pdata), GFP_KERNEL);
Gigi Joseph46d0d332015-01-09 03:45:02 +0000755 if (!dt_pdata)
Colin Ian King0810f182015-05-12 22:37:23 +0100756 return NULL;
Gigi Joseph46d0d332015-01-09 03:45:02 +0000757
758 dt_property = of_get_property(np, "dev_name", &len);
759 if (dt_property)
760 memcpy(&dt_pdata->dev_name, dt_property, len);
761 of_property_read_u32(np, "nshutdown_gpio",
Dan Carpenter63e144c2015-01-15 14:42:27 +0300762 &dt_pdata->nshutdown_gpio);
763 of_property_read_u32(np, "flow_cntrl", &dt_pdata->flow_cntrl);
764 of_property_read_u32(np, "baud_rate", &dt_pdata->baud_rate);
Gigi Joseph46d0d332015-01-09 03:45:02 +0000765
766 return dt_pdata;
767}
768
Pavan Savoy27712b32012-08-03 14:49:40 -0500769static struct dentry *kim_debugfs_dir;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500770static int kim_probe(struct platform_device *pdev)
771{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500772 struct kim_data_s *kim_gdata;
Gigi Joseph46d0d332015-01-09 03:45:02 +0000773 struct ti_st_plat_data *pdata;
Matthias Kaehlckeb2997382012-11-22 23:26:14 +0100774 int err;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500775
Gigi Joseph46d0d332015-01-09 03:45:02 +0000776 if (pdev->dev.of_node)
777 pdata = get_platform_data(&pdev->dev);
778 else
779 pdata = pdev->dev.platform_data;
780
781 if (pdata == NULL) {
782 dev_err(&pdev->dev, "Platform Data is missing\n");
783 return -ENXIO;
784 }
785
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400786 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
787 /* multiple devices could exist */
788 st_kim_devices[pdev->id] = pdev;
789 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300790 /* platform's sure about existence of 1 device */
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400791 st_kim_devices[0] = pdev;
792 }
793
Pavan Savoy38d9df42010-07-08 08:55:17 -0500794 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
795 if (!kim_gdata) {
796 pr_err("no mem to allocate");
797 return -ENOMEM;
798 }
Jingoo Han9093ca82013-05-23 19:35:23 +0900799 platform_set_drvdata(pdev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500800
Matthias Kaehlckeb2997382012-11-22 23:26:14 +0100801 err = st_core_init(&kim_gdata->core_data);
802 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500803 pr_err(" ST core init failed");
Matthias Kaehlckeb2997382012-11-22 23:26:14 +0100804 err = -EIO;
805 goto err_core_init;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500806 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500807 /* refer to itself */
808 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500809
Luciano Coelhoa7e2ca12013-01-21 13:12:42 +0200810 /* Claim the chip enable nShutdown gpio from the system */
811 kim_gdata->nshutdown = pdata->nshutdown_gpio;
812 err = gpio_request(kim_gdata->nshutdown, "kim");
813 if (unlikely(err)) {
Dan Carpenter63e144c2015-01-15 14:42:27 +0300814 pr_err(" gpio %d request failed ", kim_gdata->nshutdown);
Luciano Coelhoa7e2ca12013-01-21 13:12:42 +0200815 return err;
816 }
817
818 /* Configure nShutdown GPIO as output=0 */
819 err = gpio_direction_output(kim_gdata->nshutdown, 0);
820 if (unlikely(err)) {
Dan Carpenter63e144c2015-01-15 14:42:27 +0300821 pr_err(" unable to configure gpio %d", kim_gdata->nshutdown);
Luciano Coelhoa7e2ca12013-01-21 13:12:42 +0200822 return err;
823 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500824 /* get reference of pdev for request_firmware
825 */
826 kim_gdata->kim_pdev = pdev;
827 init_completion(&kim_gdata->kim_rcvd);
828 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500829
Matthias Kaehlckeb2997382012-11-22 23:26:14 +0100830 err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
831 if (err) {
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600832 pr_err("failed to create sysfs entries");
Matthias Kaehlckeb2997382012-11-22 23:26:14 +0100833 goto err_sysfs_group;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500834 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500835
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600836 /* copying platform data */
837 strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
838 kim_gdata->flow_cntrl = pdata->flow_cntrl;
839 kim_gdata->baud_rate = pdata->baud_rate;
840 pr_info("sysfs entries created\n");
841
Pavan Savoyc1afac12010-07-28 02:25:59 -0500842 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
Robin van der Gracht37685282014-06-16 16:38:56 +0200843 if (!kim_debugfs_dir) {
Pavan Savoyc1afac12010-07-28 02:25:59 -0500844 pr_err(" debugfs entries creation failed ");
Gigi Josephc6ec0fb2015-01-09 03:47:51 +0000845 return 0;
Naveen Jaine2a53282010-06-09 03:45:33 -0500846 }
Pavan Savoyc1afac12010-07-28 02:25:59 -0500847
848 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
849 kim_gdata, &version_debugfs_fops);
850 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
851 kim_gdata, &list_debugfs_fops);
Pavan Savoy320920c2010-07-14 08:21:12 -0500852 return 0;
Matthias Kaehlckeb2997382012-11-22 23:26:14 +0100853
Matthias Kaehlckeb2997382012-11-22 23:26:14 +0100854err_sysfs_group:
855 st_core_exit(kim_gdata->core_data);
856
857err_core_init:
858 kfree(kim_gdata);
859
860 return err;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500861}
862
863static int kim_remove(struct platform_device *pdev)
864{
Luciano Coelhoa7e2ca12013-01-21 13:12:42 +0200865 /* free the GPIOs requested */
Gigi Joseph46d0d332015-01-09 03:45:02 +0000866 struct ti_st_plat_data *pdata;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500867 struct kim_data_s *kim_gdata;
868
Gigi Joseph46d0d332015-01-09 03:45:02 +0000869 if (pdev->dev.of_node) {
870 pr_debug("use device tree data");
871 pdata = dt_pdata;
872 } else {
873 pdata = pdev->dev.platform_data;
874 }
875
Jingoo Han9093ca82013-05-23 19:35:23 +0900876 kim_gdata = platform_get_drvdata(pdev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500877
Luciano Coelhoa7e2ca12013-01-21 13:12:42 +0200878 /* Free the Bluetooth/FM/GPIO
879 * nShutdown gpio from the system
880 */
881 gpio_free(pdata->nshutdown_gpio);
882 pr_info("nshutdown GPIO Freed");
883
Pavan Savoy781a7392011-02-04 02:23:15 -0600884 debugfs_remove_recursive(kim_debugfs_dir);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600885 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
Pavan Savoy781a7392011-02-04 02:23:15 -0600886 pr_info("sysfs entries removed");
887
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500888 kim_gdata->kim_pdev = NULL;
889 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500890
891 kfree(kim_gdata);
892 kim_gdata = NULL;
Gigi Joseph46d0d332015-01-09 03:45:02 +0000893 kfree(dt_pdata);
894 dt_pdata = NULL;
895
Pavan Savoy320920c2010-07-14 08:21:12 -0500896 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500897}
898
Pavan Savoy27712b32012-08-03 14:49:40 -0500899static int kim_suspend(struct platform_device *pdev, pm_message_t state)
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600900{
Gigi Joseph46d0d332015-01-09 03:45:02 +0000901 struct ti_st_plat_data *pdata;
902
903 if (pdev->dev.of_node) {
904 pr_debug("use device tree data");
905 pdata = dt_pdata;
906 } else {
907 pdata = pdev->dev.platform_data;
908 }
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600909
910 if (pdata->suspend)
911 return pdata->suspend(pdev, state);
912
Gigi Joseph4b4aa3a2015-01-09 03:46:24 +0000913 return 0;
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600914}
915
Pavan Savoy27712b32012-08-03 14:49:40 -0500916static int kim_resume(struct platform_device *pdev)
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600917{
Gigi Joseph46d0d332015-01-09 03:45:02 +0000918 struct ti_st_plat_data *pdata;
919
920 if (pdev->dev.of_node) {
921 pr_debug("use device tree data");
922 pdata = dt_pdata;
923 } else {
924 pdata = pdev->dev.platform_data;
925 }
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600926
927 if (pdata->resume)
928 return pdata->resume(pdev);
929
Gigi Joseph4b4aa3a2015-01-09 03:46:24 +0000930 return 0;
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600931}
932
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500933/**********************************************************************/
934/* entry point for ST KIM module, called in from ST Core */
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600935static struct platform_driver kim_platform_driver = {
936 .probe = kim_probe,
937 .remove = kim_remove,
938 .suspend = kim_suspend,
939 .resume = kim_resume,
940 .driver = {
941 .name = "kim",
Gigi Joseph46d0d332015-01-09 03:45:02 +0000942 .owner = THIS_MODULE,
943 .of_match_table = of_match_ptr(kim_of_match),
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600944 },
945};
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500946
Axel Linb00e1262012-01-22 15:33:49 +0800947module_platform_driver(kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500948
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500949MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
950MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
951MODULE_LICENSE("GPL");