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