blob: 707c85826417e40523d325cb89df0ffb81507240 [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>
Pavan Savoy83ef41f2010-09-17 12:06:11 -040033#include <linux/rfkill.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050034
Pavan Savoy5c88b022011-02-04 02:23:09 -060035#include <linux/skbuff.h>
Pavan Savoye5558672010-09-30 16:13:30 -040036#include <linux/ti_wilink_st.h>
Pavan Savoy83ef41f2010-09-17 12:06:11 -040037
Pavan Savoyd0088ce2010-04-08 13:16:54 -050038
39static int kim_probe(struct platform_device *pdev);
40static int kim_remove(struct platform_device *pdev);
41
42/* KIM platform device driver structure */
43static struct platform_driver kim_platform_driver = {
44 .probe = kim_probe,
45 .remove = kim_remove,
46 /* TODO: ST driver power management during suspend/resume ?
47 */
48#if 0
49 .suspend = kim_suspend,
50 .resume = kim_resume,
51#endif
52 .driver = {
53 .name = "kim",
54 .owner = THIS_MODULE,
55 },
56};
57
Pavan Savoyd0088ce2010-04-08 13:16:54 -050058static int kim_toggle_radio(void*, bool);
59static const struct rfkill_ops kim_rfkill_ops = {
60 .set_block = kim_toggle_radio,
61};
Pavan Savoyd0088ce2010-04-08 13:16:54 -050062
63/* strings to be used for rfkill entries and by
64 * ST Core to be used for sysfs debug entry
65 */
66#define PROTO_ENTRY(type, name) name
67const unsigned char *protocol_names[] = {
68 PROTO_ENTRY(ST_BT, "Bluetooth"),
69 PROTO_ENTRY(ST_FM, "FM"),
70 PROTO_ENTRY(ST_GPS, "GPS"),
71};
72
Pavan Savoydbd3a872010-08-19 14:08:51 -040073#define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
Pavan Savoy73f12e82010-10-12 16:27:38 -040074static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
Pavan Savoyd0088ce2010-04-08 13:16:54 -050075
76/**********************************************************************/
77/* internal functions */
78
Pavan Savoy36b5aee2010-07-22 05:32:06 -050079/**
Pavan Savoydbd3a872010-08-19 14:08:51 -040080 * st_get_plat_device -
81 * function which returns the reference to the platform device
82 * requested by id. As of now only 1 such device exists (id=0)
83 * the context requesting for reference can get the id to be
84 * requested by a. The protocol driver which is registering or
85 * b. the tty device which is opened.
86 */
87static struct platform_device *st_get_plat_device(int id)
88{
89 return st_kim_devices[id];
90}
91
92/**
Pavan Savoy36b5aee2010-07-22 05:32:06 -050093 * validate_firmware_response -
94 * function to return whether the firmware response was proper
95 * in case of error don't complete so that waiting for proper
96 * response times out
Pavan Savoyd0088ce2010-04-08 13:16:54 -050097 */
Pavan Savoy38d9df42010-07-08 08:55:17 -050098void validate_firmware_response(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -050099{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500100 struct sk_buff *skb = kim_gdata->rx_skb;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500101 if (unlikely(skb->data[5] != 0)) {
102 pr_err("no proper response during fw download");
103 pr_err("data6 %x", skb->data[5]);
104 return; /* keep waiting for the proper response */
105 }
106 /* becos of all the script being downloaded */
107 complete_all(&kim_gdata->kim_rcvd);
108 kfree_skb(skb);
109}
110
111/* check for data len received inside kim_int_recv
112 * most often hit the last case to update state to waiting for data
113 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500114static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500115{
116 register int room = skb_tailroom(kim_gdata->rx_skb);
117
Pavan Savoye6d9e642010-07-22 05:32:05 -0500118 pr_debug("len %d room %d", len, room);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500119
120 if (!len) {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500121 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500122 } else if (len > room) {
123 /* Received packet's payload length is larger.
124 * We can't accommodate it in created skb.
125 */
126 pr_err("Data length is too large len %d room %d", len,
127 room);
128 kfree_skb(kim_gdata->rx_skb);
129 } else {
130 /* Packet header has non-zero payload length and
131 * we have enough space in created skb. Lets read
132 * payload data */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600133 kim_gdata->rx_state = ST_W4_DATA;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500134 kim_gdata->rx_count = len;
135 return len;
136 }
137
138 /* Change ST LL state to continue to process next
139 * packet */
140 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
141 kim_gdata->rx_skb = NULL;
142 kim_gdata->rx_count = 0;
143
144 return 0;
145}
146
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500147/**
148 * kim_int_recv - receive function called during firmware download
149 * firmware download responses on different UART drivers
150 * have been observed to come in bursts of different
151 * tty_receive and hence the logic
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500152 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500153void kim_int_recv(struct kim_data_s *kim_gdata,
154 const unsigned char *data, long count)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500155{
Pavan Savoy73f12e82010-10-12 16:27:38 -0400156 const unsigned char *ptr;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400157 int len = 0, type = 0;
Pavan Savoy5c88b022011-02-04 02:23:09 -0600158 unsigned char *plen;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500159
Pavan Savoye6d9e642010-07-22 05:32:05 -0500160 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500161 /* Decode received bytes here */
Pavan Savoy73f12e82010-10-12 16:27:38 -0400162 ptr = data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500163 if (unlikely(ptr == NULL)) {
164 pr_err(" received null from TTY ");
165 return;
166 }
Pavan Savoy73f12e82010-10-12 16:27:38 -0400167
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500168 while (count) {
169 if (kim_gdata->rx_count) {
170 len = min_t(unsigned int, kim_gdata->rx_count, count);
171 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
172 kim_gdata->rx_count -= len;
173 count -= len;
174 ptr += len;
175
176 if (kim_gdata->rx_count)
177 continue;
178
179 /* Check ST RX state machine , where are we? */
180 switch (kim_gdata->rx_state) {
181 /* Waiting for complete packet ? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600182 case ST_W4_DATA:
Pavan Savoye6d9e642010-07-22 05:32:05 -0500183 pr_debug("Complete pkt received");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500184 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500185 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
186 kim_gdata->rx_skb = NULL;
187 continue;
188 /* Waiting for Bluetooth event header ? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600189 case ST_W4_HEADER:
190 plen =
191 (unsigned char *)&kim_gdata->rx_skb->data[1];
192 pr_debug("event hdr: plen 0x%02x\n", *plen);
193 kim_check_data_len(kim_gdata, *plen);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500194 continue;
195 } /* end of switch */
196 } /* end of if rx_state */
197 switch (*ptr) {
198 /* Bluetooth event packet? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600199 case 0x04:
200 kim_gdata->rx_state = ST_W4_HEADER;
201 kim_gdata->rx_count = 2;
202 type = *ptr;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500203 break;
204 default:
205 pr_info("unknown packet");
206 ptr++;
207 count--;
208 continue;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500209 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500210 ptr++;
211 count--;
212 kim_gdata->rx_skb =
Pavan Savoy5c88b022011-02-04 02:23:09 -0600213 alloc_skb(1024+8, GFP_ATOMIC);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500214 if (!kim_gdata->rx_skb) {
215 pr_err("can't allocate mem for new packet");
216 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
217 kim_gdata->rx_count = 0;
218 return;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500219 }
Pavan Savoy5c88b022011-02-04 02:23:09 -0600220 skb_reserve(kim_gdata->rx_skb, 8);
221 kim_gdata->rx_skb->cb[0] = 4;
222 kim_gdata->rx_skb->cb[1] = 0;
223
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500224 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500225 return;
226}
227
Pavan Savoy38d9df42010-07-08 08:55:17 -0500228static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500229{
230 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400231 const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500232
Pavan Savoye6d9e642010-07-22 05:32:05 -0500233 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500234
235 INIT_COMPLETION(kim_gdata->kim_rcvd);
236 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
237 pr_err("kim: couldn't write 4 bytes");
Pavan Savoy320920c2010-07-14 08:21:12 -0500238 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500239 }
240
241 if (!wait_for_completion_timeout
242 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
243 pr_err(" waiting for ver info- timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500244 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500245 }
246
247 version =
Naveen Jaine2a53282010-06-09 03:45:33 -0500248 MAKEWORD(kim_gdata->resp_buffer[13],
249 kim_gdata->resp_buffer[14]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500250 chip = (version & 0x7C00) >> 10;
251 min_ver = (version & 0x007F);
252 maj_ver = (version & 0x0380) >> 7;
253
254 if (version & 0x8000)
255 maj_ver |= 0x0008;
256
257 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
Naveen Jaine2a53282010-06-09 03:45:33 -0500258
259 /* to be accessed later via sysfs entry */
260 kim_gdata->version.full = version;
261 kim_gdata->version.chip = chip;
262 kim_gdata->version.maj_ver = maj_ver;
263 kim_gdata->version.min_ver = min_ver;
264
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500265 pr_info("%s", bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500266 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500267}
268
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500269/**
270 * download_firmware -
271 * internal function which parses through the .bts firmware
272 * script file intreprets SEND, DELAY actions only as of now
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500273 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500274static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500275{
Pavan Savoy320920c2010-07-14 08:21:12 -0500276 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500277 long len = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400278 unsigned char *ptr = NULL;
279 unsigned char *action_ptr = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500280 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
281
Pavan Savoy38d9df42010-07-08 08:55:17 -0500282 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500283 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500284 pr_err("kim: failed to read local ver");
285 return err;
286 }
287 err =
288 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
289 &kim_gdata->kim_pdev->dev);
290 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
291 (kim_gdata->fw_entry->size == 0))) {
292 pr_err(" request_firmware failed(errno %ld) for %s", err,
293 bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500294 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500295 }
296 ptr = (void *)kim_gdata->fw_entry->data;
297 len = kim_gdata->fw_entry->size;
298 /* bts_header to remove out magic number and
299 * version
300 */
301 ptr += sizeof(struct bts_header);
302 len -= sizeof(struct bts_header);
303
304 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500305 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500306 ((struct bts_action *)ptr)->size,
307 ((struct bts_action *)ptr)->type);
308
309 switch (((struct bts_action *)ptr)->type) {
310 case ACTION_SEND_COMMAND: /* action send */
311 action_ptr = &(((struct bts_action *)ptr)->data[0]);
312 if (unlikely
313 (((struct hci_command *)action_ptr)->opcode ==
314 0xFF36)) {
315 /* ignore remote change
316 * baud rate HCI VS command */
317 pr_err
Pavan Savoye6d9e642010-07-22 05:32:05 -0500318 (" change remote baud"
319 " rate command in firmware");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500320 break;
321 }
322
323 INIT_COMPLETION(kim_gdata->kim_rcvd);
324 err = st_int_write(kim_gdata->core_data,
325 ((struct bts_action_send *)action_ptr)->data,
326 ((struct bts_action *)ptr)->size);
327 if (unlikely(err < 0)) {
328 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500329 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500330 }
331 if (!wait_for_completion_timeout
332 (&kim_gdata->kim_rcvd,
333 msecs_to_jiffies(CMD_RESP_TIME))) {
334 pr_err
335 (" response timeout during fw download ");
336 /* timed out */
337 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500338 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500339 }
340 break;
341 case ACTION_DELAY: /* sleep */
342 pr_info("sleep command in scr");
343 action_ptr = &(((struct bts_action *)ptr)->data[0]);
344 mdelay(((struct bts_action_delay *)action_ptr)->msec);
345 break;
346 }
347 len =
348 len - (sizeof(struct bts_action) +
349 ((struct bts_action *)ptr)->size);
350 ptr =
351 ptr + sizeof(struct bts_action) +
352 ((struct bts_action *)ptr)->size;
353 }
354 /* fw download complete */
355 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500356 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500357}
358
359/**********************************************************************/
360/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500361/* function to toggle the GPIO
362 * needs to know whether the GPIO is active high or active low
363 */
364void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
365{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500366 struct platform_device *kim_pdev;
367 struct kim_data_s *kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500368 pr_info(" %s ", __func__);
369
Pavan Savoydbd3a872010-08-19 14:08:51 -0400370 kim_pdev = st_get_plat_device(0);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500371 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
372
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500373 if (kim_gdata->gpios[type] == -1) {
374 pr_info(" gpio not requested for protocol %s",
375 protocol_names[type]);
376 return;
377 }
378 switch (type) {
379 case ST_BT:
380 /*Do Nothing */
381 break;
382
383 case ST_FM:
384 if (state == KIM_GPIO_ACTIVE)
385 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
386 else
387 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
388 break;
389
390 case ST_GPS:
391 if (state == KIM_GPIO_ACTIVE)
392 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
393 else
394 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
395 break;
396
Pavan Savoy5c88b022011-02-04 02:23:09 -0600397 case ST_MAX_CHANNELS:
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500398 default:
399 break;
400 }
401
402 return;
403}
404
405/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
406 * can be because of
407 * 1. response to read local version
408 * 2. during send/recv's of firmware download
409 */
410void st_kim_recv(void *disc_data, const unsigned char *data, long count)
411{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500412 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
413 struct kim_data_s *kim_gdata = st_gdata->kim_data;
414
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500415 /* copy to local buffer */
416 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
417 /* must be the read_ver_cmd */
418 memcpy(kim_gdata->resp_buffer, data, count);
419 complete_all(&kim_gdata->kim_rcvd);
420 return;
421 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500422 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500423 /* either completes or times out */
424 }
425 return;
426}
427
428/* to signal completion of line discipline installation
429 * called from ST Core, upon tty_open
430 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500431void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500432{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500433 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500434 complete(&kim_gdata->ldisc_installed);
435}
436
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500437/**
438 * st_kim_start - called from ST Core upon 1st registration
439 * This involves toggling the chip enable gpio, reading
440 * the firmware version from chip, forming the fw file name
441 * based on the chip version, requesting the fw, parsing it
442 * and perform download(send/recv).
443 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500444long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500445{
Pavan Savoy320920c2010-07-14 08:21:12 -0500446 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500447 long retry = POR_RETRY_COUNT;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500448 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
449
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500450 pr_info(" %s", __func__);
451
452 do {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500453 /* TODO: this is only because rfkill sub-system
454 * doesn't send events to user-space if the state
455 * isn't changed
456 */
457 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500458 /* Configure BT nShutdown to HIGH state */
459 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
460 mdelay(5); /* FIXME: a proper toggle */
461 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
462 mdelay(100);
463 /* re-initialize the completion */
464 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500465#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500466 /* send signal to UIM */
467 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
468 if (err != 0) {
469 pr_info(" sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500470 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500471 continue;
472 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500473#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500474 /* unblock and send event to UIM via /dev/rfkill */
475 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500476 /* wait for ldisc to be installed */
477 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
478 msecs_to_jiffies(LDISC_TIME));
479 if (!err) { /* timeout */
480 pr_err("line disc installation timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500481 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500482 continue;
483 } else {
484 /* ldisc installed now */
485 pr_info(" line discipline installed ");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500486 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500487 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500488 pr_err("download firmware failed");
489 continue;
490 } else { /* on success don't retry */
491 break;
492 }
493 }
494 } while (retry--);
495 return err;
496}
497
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500498/**
499 * st_kim_stop - called from ST Core, on the last un-registration
500 * toggle low the chip enable gpio
501 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500502long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500503{
Pavan Savoy320920c2010-07-14 08:21:12 -0500504 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500505 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500506
507 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500508#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500509 /* send signal to UIM */
510 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
511 if (err != 0) {
512 pr_err("sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500513 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500514 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500515#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500516 /* set BT rfkill to be blocked */
517 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500518
519 /* wait for ldisc to be un-installed */
520 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
521 msecs_to_jiffies(LDISC_TIME));
522 if (!err) { /* timeout */
523 pr_err(" timed out waiting for ldisc to be un-installed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500524 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500525 }
526
527 /* By default configure BT nShutdown to LOW state */
528 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
529 mdelay(1);
530 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
531 mdelay(1);
532 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
533 return err;
534}
535
536/**********************************************************************/
537/* functions called from subsystems */
Pavan Savoyc1afac12010-07-28 02:25:59 -0500538/* called when debugfs entry is read from */
Naveen Jaine2a53282010-06-09 03:45:33 -0500539
Pavan Savoyc1afac12010-07-28 02:25:59 -0500540static int show_version(struct seq_file *s, void *unused)
Naveen Jaine2a53282010-06-09 03:45:33 -0500541{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500542 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
543 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
Naveen Jaine2a53282010-06-09 03:45:33 -0500544 kim_gdata->version.chip, kim_gdata->version.maj_ver,
545 kim_gdata->version.min_ver);
Pavan Savoyc1afac12010-07-28 02:25:59 -0500546 return 0;
Naveen Jaine2a53282010-06-09 03:45:33 -0500547}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500548
Pavan Savoyc1afac12010-07-28 02:25:59 -0500549static int show_list(struct seq_file *s, void *unused)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500550{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500551 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
552 kim_st_list_protocols(kim_gdata->core_data, s);
553 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500554}
555
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500556/* function called from rfkill subsystem, when someone from
557 * user space would write 0/1 on the sysfs entry
558 * /sys/class/rfkill/rfkill0,1,3/state
559 */
560static int kim_toggle_radio(void *data, bool blocked)
561{
562 enum proto_type type = *((enum proto_type *)data);
Pavan Savoye6d9e642010-07-22 05:32:05 -0500563 pr_debug(" %s: %d ", __func__, type);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500564
565 switch (type) {
566 case ST_BT:
567 /* do nothing */
568 break;
569 case ST_FM:
570 case ST_GPS:
571 if (blocked)
572 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
573 else
574 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
575 break;
Pavan Savoy5c88b022011-02-04 02:23:09 -0600576 case ST_MAX_CHANNELS:
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500577 pr_err(" wrong proto type ");
578 break;
579 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500580 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500581}
582
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500583/**
584 * st_kim_ref - reference the core's data
585 * This references the per-ST platform device in the arch/xx/
586 * board-xx.c file.
587 * This would enable multiple such platform devices to exist
588 * on a given platform
589 */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400590void st_kim_ref(struct st_data_s **core_data, int id)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500591{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500592 struct platform_device *pdev;
593 struct kim_data_s *kim_gdata;
594 /* get kim_gdata reference from platform device */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400595 pdev = st_get_plat_device(id);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500596 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500597 *core_data = kim_gdata->core_data;
598}
599
Pavan Savoyc1afac12010-07-28 02:25:59 -0500600static int kim_version_open(struct inode *i, struct file *f)
601{
602 return single_open(f, show_version, i->i_private);
603}
604
605static int kim_list_open(struct inode *i, struct file *f)
606{
607 return single_open(f, show_list, i->i_private);
608}
609
610static const struct file_operations version_debugfs_fops = {
611 /* version info */
612 .open = kim_version_open,
613 .read = seq_read,
614 .llseek = seq_lseek,
615 .release = single_release,
616};
617static const struct file_operations list_debugfs_fops = {
618 /* protocols info */
619 .open = kim_list_open,
620 .read = seq_read,
621 .llseek = seq_lseek,
622 .release = single_release,
623};
624
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500625/**********************************************************************/
626/* functions called from platform device driver subsystem
627 * need to have a relevant platform device entry in the platform's
628 * board-*.c file
629 */
630
Pavan Savoyc1afac12010-07-28 02:25:59 -0500631struct dentry *kim_debugfs_dir;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500632static int kim_probe(struct platform_device *pdev)
633{
634 long status;
635 long proto;
636 long *gpios = pdev->dev.platform_data;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500637 struct kim_data_s *kim_gdata;
638
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400639 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
640 /* multiple devices could exist */
641 st_kim_devices[pdev->id] = pdev;
642 } else {
643 /* platform's sure about existance of 1 device */
644 st_kim_devices[0] = pdev;
645 }
646
Pavan Savoy38d9df42010-07-08 08:55:17 -0500647 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
648 if (!kim_gdata) {
649 pr_err("no mem to allocate");
650 return -ENOMEM;
651 }
652 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500653
654 status = st_core_init(&kim_gdata->core_data);
655 if (status != 0) {
656 pr_err(" ST core init failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500657 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500658 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500659 /* refer to itself */
660 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500661
Pavan Savoy5c88b022011-02-04 02:23:09 -0600662 for (proto = 0; proto < ST_MAX_CHANNELS; proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500663 kim_gdata->gpios[proto] = gpios[proto];
664 pr_info(" %ld gpio to be requested", gpios[proto]);
665 }
666
Pavan Savoy5c88b022011-02-04 02:23:09 -0600667 for (proto = 0; (proto < ST_MAX_CHANNELS)
668 && (gpios[proto] != -1); proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500669 /* Claim the Bluetooth/FM/GPIO
670 * nShutdown gpio from the system
671 */
672 status = gpio_request(gpios[proto], "kim");
673 if (unlikely(status)) {
674 pr_err(" gpio %ld request failed ", gpios[proto]);
675 proto -= 1;
676 while (proto >= 0) {
677 if (gpios[proto] != -1)
678 gpio_free(gpios[proto]);
679 }
680 return status;
681 }
682
683 /* Configure nShutdown GPIO as output=0 */
684 status =
685 gpio_direction_output(gpios[proto], 0);
686 if (unlikely(status)) {
687 pr_err(" unable to configure gpio %ld",
688 gpios[proto]);
689 proto -= 1;
690 while (proto >= 0) {
691 if (gpios[proto] != -1)
692 gpio_free(gpios[proto]);
693 }
694 return status;
695 }
696 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500697 /* get reference of pdev for request_firmware
698 */
699 kim_gdata->kim_pdev = pdev;
700 init_completion(&kim_gdata->kim_rcvd);
701 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500702
Pavan Savoy5c88b022011-02-04 02:23:09 -0600703 for (proto = 0; (proto < ST_MAX_CHANNELS)
704 && (gpios[proto] != -1); proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500705 /* TODO: should all types be rfkill_type_bt ? */
706 kim_gdata->rf_protos[proto] = proto;
707 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
708 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
709 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
710 if (kim_gdata->rfkill[proto] == NULL) {
711 pr_err("cannot create rfkill entry for gpio %ld",
712 gpios[proto]);
713 continue;
714 }
715 /* block upon creation */
716 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
717 status = rfkill_register(kim_gdata->rfkill[proto]);
718 if (unlikely(status)) {
719 pr_err("rfkill registration failed for gpio %ld",
720 gpios[proto]);
721 rfkill_unregister(kim_gdata->rfkill[proto]);
722 continue;
723 }
724 pr_info("rfkill entry created for %ld", gpios[proto]);
725 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500726
Pavan Savoyc1afac12010-07-28 02:25:59 -0500727 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
728 if (IS_ERR(kim_debugfs_dir)) {
729 pr_err(" debugfs entries creation failed ");
730 kim_debugfs_dir = NULL;
Naveen Jaine2a53282010-06-09 03:45:33 -0500731 return -1;
732 }
Pavan Savoyc1afac12010-07-28 02:25:59 -0500733
734 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
735 kim_gdata, &version_debugfs_fops);
736 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
737 kim_gdata, &list_debugfs_fops);
738 pr_info(" debugfs entries created ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500739 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500740}
741
742static int kim_remove(struct platform_device *pdev)
743{
744 /* free the GPIOs requested
745 */
746 long *gpios = pdev->dev.platform_data;
747 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500748 struct kim_data_s *kim_gdata;
749
750 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500751
Pavan Savoy5c88b022011-02-04 02:23:09 -0600752 for (proto = 0; (proto < ST_MAX_CHANNELS)
753 && (gpios[proto] != -1); proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500754 /* Claim the Bluetooth/FM/GPIO
755 * nShutdown gpio from the system
756 */
757 gpio_free(gpios[proto]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500758 rfkill_unregister(kim_gdata->rfkill[proto]);
759 rfkill_destroy(kim_gdata->rfkill[proto]);
760 kim_gdata->rfkill[proto] = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500761 }
762 pr_info("kim: GPIO Freed");
Pavan Savoyc1afac12010-07-28 02:25:59 -0500763 debugfs_remove_recursive(kim_debugfs_dir);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500764 kim_gdata->kim_pdev = NULL;
765 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500766
767 kfree(kim_gdata);
768 kim_gdata = NULL;
Pavan Savoy320920c2010-07-14 08:21:12 -0500769 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500770}
771
772/**********************************************************************/
773/* entry point for ST KIM module, called in from ST Core */
774
775static int __init st_kim_init(void)
776{
Pavan Savoy320920c2010-07-14 08:21:12 -0500777 long ret = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500778 ret = platform_driver_register(&kim_platform_driver);
779 if (ret != 0) {
780 pr_err("platform drv registration failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500781 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500782 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500783 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500784}
785
786static void __exit st_kim_deinit(void)
787{
788 /* the following returns void */
789 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500790}
791
792
793module_init(st_kim_init);
794module_exit(st_kim_deinit);
795MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
796MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
797MODULE_LICENSE("GPL");