linux/drivers/iio/common/ssp_sensors/ssp_spi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
   4 */
   5
   6#include "ssp.h"
   7
   8#define SSP_DEV (&data->spi->dev)
   9#define SSP_GET_MESSAGE_TYPE(data) (data & (3 << SSP_RW))
  10
  11/*
  12 * SSP -> AP Instruction
  13 * They tell what packet type can be expected. In the future there will
  14 * be less of them. BYPASS means common sensor packets with accel, gyro,
  15 * hrm etc. data. LIBRARY and META are mock-up's for now.
  16 */
  17#define SSP_MSG2AP_INST_BYPASS_DATA             0x37
  18#define SSP_MSG2AP_INST_LIBRARY_DATA            0x01
  19#define SSP_MSG2AP_INST_DEBUG_DATA              0x03
  20#define SSP_MSG2AP_INST_BIG_DATA                0x04
  21#define SSP_MSG2AP_INST_META_DATA               0x05
  22#define SSP_MSG2AP_INST_TIME_SYNC               0x06
  23#define SSP_MSG2AP_INST_RESET                   0x07
  24
  25#define SSP_UNIMPLEMENTED -1
  26
  27struct ssp_msg_header {
  28        u8 cmd;
  29        __le16 length;
  30        __le16 options;
  31        __le32 data;
  32} __attribute__((__packed__));
  33
  34struct ssp_msg {
  35        u16 length;
  36        u16 options;
  37        struct list_head list;
  38        struct completion *done;
  39        struct ssp_msg_header *h;
  40        char *buffer;
  41};
  42
  43static const int ssp_offset_map[SSP_SENSOR_MAX] = {
  44        [SSP_ACCELEROMETER_SENSOR] =            SSP_ACCELEROMETER_SIZE +
  45                                                SSP_TIME_SIZE,
  46        [SSP_GYROSCOPE_SENSOR] =                SSP_GYROSCOPE_SIZE +
  47                                                SSP_TIME_SIZE,
  48        [SSP_GEOMAGNETIC_UNCALIB_SENSOR] =      SSP_UNIMPLEMENTED,
  49        [SSP_GEOMAGNETIC_RAW] =                 SSP_UNIMPLEMENTED,
  50        [SSP_GEOMAGNETIC_SENSOR] =              SSP_UNIMPLEMENTED,
  51        [SSP_PRESSURE_SENSOR] =                 SSP_UNIMPLEMENTED,
  52        [SSP_GESTURE_SENSOR] =                  SSP_UNIMPLEMENTED,
  53        [SSP_PROXIMITY_SENSOR] =                SSP_UNIMPLEMENTED,
  54        [SSP_TEMPERATURE_HUMIDITY_SENSOR] =     SSP_UNIMPLEMENTED,
  55        [SSP_LIGHT_SENSOR] =                    SSP_UNIMPLEMENTED,
  56        [SSP_PROXIMITY_RAW] =                   SSP_UNIMPLEMENTED,
  57        [SSP_ORIENTATION_SENSOR] =              SSP_UNIMPLEMENTED,
  58        [SSP_STEP_DETECTOR] =                   SSP_UNIMPLEMENTED,
  59        [SSP_SIG_MOTION_SENSOR] =               SSP_UNIMPLEMENTED,
  60        [SSP_GYRO_UNCALIB_SENSOR] =             SSP_UNIMPLEMENTED,
  61        [SSP_GAME_ROTATION_VECTOR] =            SSP_UNIMPLEMENTED,
  62        [SSP_ROTATION_VECTOR] =                 SSP_UNIMPLEMENTED,
  63        [SSP_STEP_COUNTER] =                    SSP_UNIMPLEMENTED,
  64        [SSP_BIO_HRM_RAW] =                     SSP_BIO_HRM_RAW_SIZE +
  65                                                SSP_TIME_SIZE,
  66        [SSP_BIO_HRM_RAW_FAC] =                 SSP_BIO_HRM_RAW_FAC_SIZE +
  67                                                SSP_TIME_SIZE,
  68        [SSP_BIO_HRM_LIB] =                     SSP_BIO_HRM_LIB_SIZE +
  69                                                SSP_TIME_SIZE,
  70};
  71
  72#define SSP_HEADER_SIZE         (sizeof(struct ssp_msg_header))
  73#define SSP_HEADER_SIZE_ALIGNED (ALIGN(SSP_HEADER_SIZE, 4))
  74
  75static struct ssp_msg *ssp_create_msg(u8 cmd, u16 len, u16 opt, u32 data)
  76{
  77        struct ssp_msg_header h;
  78        struct ssp_msg *msg;
  79
  80        msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  81        if (!msg)
  82                return NULL;
  83
  84        h.cmd = cmd;
  85        h.length = cpu_to_le16(len);
  86        h.options = cpu_to_le16(opt);
  87        h.data = cpu_to_le32(data);
  88
  89        msg->buffer = kzalloc(SSP_HEADER_SIZE_ALIGNED + len,
  90                              GFP_KERNEL | GFP_DMA);
  91        if (!msg->buffer) {
  92                kfree(msg);
  93                return NULL;
  94        }
  95
  96        msg->length = len;
  97        msg->options = opt;
  98
  99        memcpy(msg->buffer, &h, SSP_HEADER_SIZE);
 100
 101        return msg;
 102}
 103
 104/*
 105 * It is a bit heavy to do it this way but often the function is used to compose
 106 * the message from smaller chunks which are placed on the stack.  Often the
 107 * chunks are small so memcpy should be optimalized.
 108 */
 109static inline void ssp_fill_buffer(struct ssp_msg *m, unsigned int offset,
 110                                   const void *src, unsigned int len)
 111{
 112        memcpy(&m->buffer[SSP_HEADER_SIZE_ALIGNED + offset], src, len);
 113}
 114
 115static inline void ssp_get_buffer(struct ssp_msg *m, unsigned int offset,
 116                                  void *dest, unsigned int len)
 117{
 118        memcpy(dest, &m->buffer[SSP_HEADER_SIZE_ALIGNED + offset],  len);
 119}
 120
 121#define SSP_GET_BUFFER_AT_INDEX(m, index) \
 122        (m->buffer[SSP_HEADER_SIZE_ALIGNED + index])
 123#define SSP_SET_BUFFER_AT_INDEX(m, index, val) \
 124        (m->buffer[SSP_HEADER_SIZE_ALIGNED + index] = val)
 125
 126static void ssp_clean_msg(struct ssp_msg *m)
 127{
 128        kfree(m->buffer);
 129        kfree(m);
 130}
 131
 132static int ssp_print_mcu_debug(char *data_frame, int *data_index,
 133                               int received_len)
 134{
 135        int length = data_frame[(*data_index)++];
 136
 137        if (length > received_len - *data_index || length <= 0) {
 138                ssp_dbg("[SSP]: MSG From MCU-invalid debug length(%d/%d)\n",
 139                        length, received_len);
 140                return length ? length : -EPROTO;
 141        }
 142
 143        ssp_dbg("[SSP]: MSG From MCU - %s\n", &data_frame[*data_index]);
 144
 145        *data_index += length;
 146
 147        return 0;
 148}
 149
 150/*
 151 * It was designed that way - additional lines to some kind of handshake,
 152 * please do not ask why - only the firmware guy can know it.
 153 */
 154static int ssp_check_lines(struct ssp_data *data, bool state)
 155{
 156        int delay_cnt = 0;
 157
 158        gpiod_set_value_cansleep(data->ap_mcu_gpiod, state);
 159
 160        while (gpiod_get_value_cansleep(data->mcu_ap_gpiod) != state) {
 161                usleep_range(3000, 3500);
 162
 163                if (data->shut_down || delay_cnt++ > 500) {
 164                        dev_err(SSP_DEV, "%s:timeout, hw ack wait fail %d\n",
 165                                __func__, state);
 166
 167                        if (!state)
 168                                gpiod_set_value_cansleep(data->ap_mcu_gpiod, 1);
 169
 170                        return -ETIMEDOUT;
 171                }
 172        }
 173
 174        return 0;
 175}
 176
 177static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg,
 178                           struct completion *done, int timeout)
 179{
 180        int status;
 181        /*
 182         * check if this is a short one way message or the whole transfer has
 183         * second part after an interrupt
 184         */
 185        const bool use_no_irq = msg->length == 0;
 186
 187        if (data->shut_down)
 188                return -EPERM;
 189
 190        msg->done = done;
 191
 192        mutex_lock(&data->comm_lock);
 193
 194        status = ssp_check_lines(data, false);
 195        if (status < 0)
 196                goto _error_locked;
 197
 198        status = spi_write(data->spi, msg->buffer, SSP_HEADER_SIZE);
 199        if (status < 0) {
 200                gpiod_set_value_cansleep(data->ap_mcu_gpiod, 1);
 201                dev_err(SSP_DEV, "%s spi_write fail\n", __func__);
 202                goto _error_locked;
 203        }
 204
 205        if (!use_no_irq) {
 206                mutex_lock(&data->pending_lock);
 207                list_add_tail(&msg->list, &data->pending_list);
 208                mutex_unlock(&data->pending_lock);
 209        }
 210
 211        status = ssp_check_lines(data, true);
 212        if (status < 0) {
 213                if (!use_no_irq) {
 214                        mutex_lock(&data->pending_lock);
 215                        list_del(&msg->list);
 216                        mutex_unlock(&data->pending_lock);
 217                }
 218                goto _error_locked;
 219        }
 220
 221        mutex_unlock(&data->comm_lock);
 222
 223        if (!use_no_irq && done)
 224                if (wait_for_completion_timeout(done,
 225                                                msecs_to_jiffies(timeout)) ==
 226                    0) {
 227                        mutex_lock(&data->pending_lock);
 228                        list_del(&msg->list);
 229                        mutex_unlock(&data->pending_lock);
 230
 231                        data->timeout_cnt++;
 232                        return -ETIMEDOUT;
 233                }
 234
 235        return 0;
 236
 237_error_locked:
 238        mutex_unlock(&data->comm_lock);
 239        data->timeout_cnt++;
 240        return status;
 241}
 242
 243static inline int ssp_spi_sync_command(struct ssp_data *data,
 244                                       struct ssp_msg *msg)
 245{
 246        return ssp_do_transfer(data, msg, NULL, 0);
 247}
 248
 249static int ssp_spi_sync(struct ssp_data *data, struct ssp_msg *msg,
 250                        int timeout)
 251{
 252        DECLARE_COMPLETION_ONSTACK(done);
 253
 254        if (WARN_ON(!msg->length))
 255                return -EPERM;
 256
 257        return ssp_do_transfer(data, msg, &done, timeout);
 258}
 259
 260static int ssp_handle_big_data(struct ssp_data *data, char *dataframe, int *idx)
 261{
 262        /* mock-up, it will be changed with adding another sensor types */
 263        *idx += 8;
 264        return 0;
 265}
 266
 267static int ssp_parse_dataframe(struct ssp_data *data, char *dataframe, int len)
 268{
 269        int idx, sd;
 270        struct ssp_sensor_data *spd;
 271        struct iio_dev **indio_devs = data->sensor_devs;
 272
 273        for (idx = 0; idx < len;) {
 274                switch (dataframe[idx++]) {
 275                case SSP_MSG2AP_INST_BYPASS_DATA:
 276                        sd = dataframe[idx++];
 277                        if (sd < 0 || sd >= SSP_SENSOR_MAX) {
 278                                dev_err(SSP_DEV,
 279                                        "Mcu data frame1 error %d\n", sd);
 280                                return -EPROTO;
 281                        }
 282
 283                        if (indio_devs[sd]) {
 284                                spd = iio_priv(indio_devs[sd]);
 285                                if (spd->process_data)
 286                                        spd->process_data(indio_devs[sd],
 287                                                          &dataframe[idx],
 288                                                          data->timestamp);
 289                        } else {
 290                                dev_err(SSP_DEV, "no client for frame\n");
 291                        }
 292
 293                        idx += ssp_offset_map[sd];
 294                        break;
 295                case SSP_MSG2AP_INST_DEBUG_DATA:
 296                        sd = ssp_print_mcu_debug(dataframe, &idx, len);
 297                        if (sd) {
 298                                dev_err(SSP_DEV,
 299                                        "Mcu data frame3 error %d\n", sd);
 300                                return sd;
 301                        }
 302                        break;
 303                case SSP_MSG2AP_INST_LIBRARY_DATA:
 304                        idx += len;
 305                        break;
 306                case SSP_MSG2AP_INST_BIG_DATA:
 307                        ssp_handle_big_data(data, dataframe, &idx);
 308                        break;
 309                case SSP_MSG2AP_INST_TIME_SYNC:
 310                        data->time_syncing = true;
 311                        break;
 312                case SSP_MSG2AP_INST_RESET:
 313                        ssp_queue_ssp_refresh_task(data, 0);
 314                        break;
 315                }
 316        }
 317
 318        if (data->time_syncing)
 319                data->timestamp = ktime_get_real_ns();
 320
 321        return 0;
 322}
 323
 324/* threaded irq */
 325int ssp_irq_msg(struct ssp_data *data)
 326{
 327        bool found = false;
 328        char *buffer;
 329        u8 msg_type;
 330        int ret;
 331        u16 length, msg_options;
 332        struct ssp_msg *msg, *n;
 333
 334        ret = spi_read(data->spi, data->header_buffer, SSP_HEADER_BUFFER_SIZE);
 335        if (ret < 0) {
 336                dev_err(SSP_DEV, "header read fail\n");
 337                return ret;
 338        }
 339
 340        length = le16_to_cpu(data->header_buffer[1]);
 341        msg_options = le16_to_cpu(data->header_buffer[0]);
 342
 343        if (length == 0) {
 344                dev_err(SSP_DEV, "length received from mcu is 0\n");
 345                return -EINVAL;
 346        }
 347
 348        msg_type = SSP_GET_MESSAGE_TYPE(msg_options);
 349
 350        switch (msg_type) {
 351        case SSP_AP2HUB_READ:
 352        case SSP_AP2HUB_WRITE:
 353                /*
 354                 * this is a small list, a few elements - the packets can be
 355                 * received with no order
 356                 */
 357                mutex_lock(&data->pending_lock);
 358                list_for_each_entry_safe(msg, n, &data->pending_list, list) {
 359                        if (msg->options == msg_options) {
 360                                list_del(&msg->list);
 361                                found = true;
 362                                break;
 363                        }
 364                }
 365
 366                if (!found) {
 367                        /*
 368                         * here can be implemented dead messages handling
 369                         * but the slave should not send such ones - it is to
 370                         * check but let's handle this
 371                         */
 372                        buffer = kmalloc(length, GFP_KERNEL | GFP_DMA);
 373                        if (!buffer) {
 374                                ret = -ENOMEM;
 375                                goto _unlock;
 376                        }
 377
 378                        /* got dead packet so it is always an error */
 379                        ret = spi_read(data->spi, buffer, length);
 380                        if (ret >= 0)
 381                                ret = -EPROTO;
 382
 383                        kfree(buffer);
 384
 385                        dev_err(SSP_DEV, "No match error %x\n",
 386                                msg_options);
 387
 388                        goto _unlock;
 389                }
 390
 391                if (msg_type == SSP_AP2HUB_READ)
 392                        ret = spi_read(data->spi,
 393                                       &msg->buffer[SSP_HEADER_SIZE_ALIGNED],
 394                                       msg->length);
 395
 396                if (msg_type == SSP_AP2HUB_WRITE) {
 397                        ret = spi_write(data->spi,
 398                                        &msg->buffer[SSP_HEADER_SIZE_ALIGNED],
 399                                        msg->length);
 400                        if (msg_options & SSP_AP2HUB_RETURN) {
 401                                msg->options =
 402                                        SSP_AP2HUB_READ | SSP_AP2HUB_RETURN;
 403                                msg->length = 1;
 404
 405                                list_add_tail(&msg->list, &data->pending_list);
 406                                goto _unlock;
 407                        }
 408                }
 409
 410                if (msg->done)
 411                        if (!completion_done(msg->done))
 412                                complete(msg->done);
 413_unlock:
 414                mutex_unlock(&data->pending_lock);
 415                break;
 416        case SSP_HUB2AP_WRITE:
 417                buffer = kzalloc(length, GFP_KERNEL | GFP_DMA);
 418                if (!buffer)
 419                        return -ENOMEM;
 420
 421                ret = spi_read(data->spi, buffer, length);
 422                if (ret < 0) {
 423                        dev_err(SSP_DEV, "spi read fail\n");
 424                        kfree(buffer);
 425                        break;
 426                }
 427
 428                ret = ssp_parse_dataframe(data, buffer, length);
 429
 430                kfree(buffer);
 431                break;
 432
 433        default:
 434                dev_err(SSP_DEV, "unknown msg type\n");
 435                return -EPROTO;
 436        }
 437
 438        return ret;
 439}
 440
 441void ssp_clean_pending_list(struct ssp_data *data)
 442{
 443        struct ssp_msg *msg, *n;
 444
 445        mutex_lock(&data->pending_lock);
 446        list_for_each_entry_safe(msg, n, &data->pending_list, list) {
 447                list_del(&msg->list);
 448
 449                if (msg->done)
 450                        if (!completion_done(msg->done))
 451                                complete(msg->done);
 452        }
 453        mutex_unlock(&data->pending_lock);
 454}
 455
 456int ssp_command(struct ssp_data *data, char command, int arg)
 457{
 458        int ret;
 459        struct ssp_msg *msg;
 460
 461        msg = ssp_create_msg(command, 0, SSP_AP2HUB_WRITE, arg);
 462        if (!msg)
 463                return -ENOMEM;
 464
 465        ssp_dbg("%s - command 0x%x %d\n", __func__, command, arg);
 466
 467        ret = ssp_spi_sync_command(data, msg);
 468        ssp_clean_msg(msg);
 469
 470        return ret;
 471}
 472
 473int ssp_send_instruction(struct ssp_data *data, u8 inst, u8 sensor_type,
 474                         u8 *send_buf, u8 length)
 475{
 476        int ret;
 477        struct ssp_msg *msg;
 478
 479        if (data->fw_dl_state == SSP_FW_DL_STATE_DOWNLOADING) {
 480                dev_err(SSP_DEV, "%s - Skip Inst! DL state = %d\n",
 481                        __func__, data->fw_dl_state);
 482                return -EBUSY;
 483        } else if (!(data->available_sensors & BIT(sensor_type)) &&
 484                   (inst <= SSP_MSG2SSP_INST_CHANGE_DELAY)) {
 485                dev_err(SSP_DEV, "%s - Bypass Inst Skip! - %u\n",
 486                        __func__, sensor_type);
 487                return -EIO; /* just fail */
 488        }
 489
 490        msg = ssp_create_msg(inst, length + 2, SSP_AP2HUB_WRITE, 0);
 491        if (!msg)
 492                return -ENOMEM;
 493
 494        ssp_fill_buffer(msg, 0, &sensor_type, 1);
 495        ssp_fill_buffer(msg, 1, send_buf, length);
 496
 497        ssp_dbg("%s - Inst = 0x%x, Sensor Type = 0x%x, data = %u\n",
 498                __func__, inst, sensor_type, send_buf[1]);
 499
 500        ret = ssp_spi_sync(data, msg, 1000);
 501        ssp_clean_msg(msg);
 502
 503        return ret;
 504}
 505
 506int ssp_get_chipid(struct ssp_data *data)
 507{
 508        int ret;
 509        char buffer;
 510        struct ssp_msg *msg;
 511
 512        msg = ssp_create_msg(SSP_MSG2SSP_AP_WHOAMI, 1, SSP_AP2HUB_READ, 0);
 513        if (!msg)
 514                return -ENOMEM;
 515
 516        ret = ssp_spi_sync(data, msg, 1000);
 517
 518        buffer = SSP_GET_BUFFER_AT_INDEX(msg, 0);
 519
 520        ssp_clean_msg(msg);
 521
 522        return ret < 0 ? ret : buffer;
 523}
 524
 525int ssp_set_magnetic_matrix(struct ssp_data *data)
 526{
 527        int ret;
 528        struct ssp_msg *msg;
 529
 530        msg = ssp_create_msg(SSP_MSG2SSP_AP_SET_MAGNETIC_STATIC_MATRIX,
 531                             data->sensorhub_info->mag_length, SSP_AP2HUB_WRITE,
 532                             0);
 533        if (!msg)
 534                return -ENOMEM;
 535
 536        ssp_fill_buffer(msg, 0, data->sensorhub_info->mag_table,
 537                        data->sensorhub_info->mag_length);
 538
 539        ret = ssp_spi_sync(data, msg, 1000);
 540        ssp_clean_msg(msg);
 541
 542        return ret;
 543}
 544
 545unsigned int ssp_get_sensor_scanning_info(struct ssp_data *data)
 546{
 547        int ret;
 548        __le32 result;
 549        u32 cpu_result = 0;
 550
 551        struct ssp_msg *msg = ssp_create_msg(SSP_MSG2SSP_AP_SENSOR_SCANNING, 4,
 552                                             SSP_AP2HUB_READ, 0);
 553        if (!msg)
 554                return 0;
 555
 556        ret = ssp_spi_sync(data, msg, 1000);
 557        if (ret < 0) {
 558                dev_err(SSP_DEV, "%s - spi read fail %d\n", __func__, ret);
 559                goto _exit;
 560        }
 561
 562        ssp_get_buffer(msg, 0, &result, 4);
 563        cpu_result = le32_to_cpu(result);
 564
 565        dev_info(SSP_DEV, "%s state: 0x%08x\n", __func__, cpu_result);
 566
 567_exit:
 568        ssp_clean_msg(msg);
 569        return cpu_result;
 570}
 571
 572unsigned int ssp_get_firmware_rev(struct ssp_data *data)
 573{
 574        int ret;
 575        __le32 result;
 576
 577        struct ssp_msg *msg = ssp_create_msg(SSP_MSG2SSP_AP_FIRMWARE_REV, 4,
 578                                             SSP_AP2HUB_READ, 0);
 579        if (!msg)
 580                return SSP_INVALID_REVISION;
 581
 582        ret = ssp_spi_sync(data, msg, 1000);
 583        if (ret < 0) {
 584                dev_err(SSP_DEV, "%s - transfer fail %d\n", __func__, ret);
 585                ret = SSP_INVALID_REVISION;
 586                goto _exit;
 587        }
 588
 589        ssp_get_buffer(msg, 0, &result, 4);
 590        ret = le32_to_cpu(result);
 591
 592_exit:
 593        ssp_clean_msg(msg);
 594        return ret;
 595}
 596