1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
6 #include <linux/iio/iio.h>
7 #include <linux/interrupt.h>
9 #include <linux/mfd/core.h>
10 #include <linux/module.h>
12 #include <linux/of_platform.h>
15 #define SSP_WDT_TIME 10000
16 #define SSP_LIMIT_RESET_CNT 20
17 #define SSP_LIMIT_TIMEOUT_CNT 3
19 /* It is possible that it is max clk rate for version 1.0 of bootcode */
20 #define SSP_BOOT_SPI_HZ 400000
23 * These fields can look enigmatic but this structure is used mainly to flat
24 * some values and depends on command type.
26 struct ssp_instruction {
30 } __attribute__((__packed__));
32 static const u8 ssp_magnitude_table[] = {110, 85, 171, 71, 203, 195, 0, 67,
33 208, 56, 175, 244, 206, 213, 0, 92, 250, 0, 55, 48, 189, 252, 171,
36 static const struct ssp_sensorhub_info ssp_rinato_info = {
37 .fw_name = "ssp_B2.fw",
38 .fw_crashed_name = "ssp_crashed.fw",
40 .mag_table = ssp_magnitude_table,
41 .mag_length = ARRAY_SIZE(ssp_magnitude_table),
44 static const struct ssp_sensorhub_info ssp_thermostat_info = {
45 .fw_name = "thermostat_B2.fw",
46 .fw_crashed_name = "ssp_crashed.fw",
48 .mag_table = ssp_magnitude_table,
49 .mag_length = ARRAY_SIZE(ssp_magnitude_table),
52 static const struct mfd_cell sensorhub_sensor_devs[] = {
54 .name = "ssp-accelerometer",
57 .name = "ssp-gyroscope",
61 static void ssp_toggle_mcu_reset_gpio(struct ssp_data *data)
63 gpiod_set_value(data->mcu_reset_gpiod, 0);
64 usleep_range(1000, 1200);
65 gpiod_set_value(data->mcu_reset_gpiod, 1);
69 static void ssp_sync_available_sensors(struct ssp_data *data)
73 for (i = 0; i < SSP_SENSOR_MAX; ++i) {
74 if (data->available_sensors & BIT(i)) {
75 ret = ssp_enable_sensor(data, i, data->delay_buf[i]);
77 dev_err(&data->spi->dev,
78 "Sync sensor nr: %d fail\n", i);
84 ret = ssp_command(data, SSP_MSG2SSP_AP_MCU_SET_DUMPMODE,
87 dev_err(&data->spi->dev,
88 "SSP_MSG2SSP_AP_MCU_SET_DUMPMODE failed\n");
91 static void ssp_enable_mcu(struct ssp_data *data, bool enable)
93 dev_info(&data->spi->dev, "current shutdown = %d, old = %d\n", enable,
96 if (enable && data->shut_down) {
97 data->shut_down = false;
98 enable_irq(data->spi->irq);
99 enable_irq_wake(data->spi->irq);
100 } else if (!enable && !data->shut_down) {
101 data->shut_down = true;
102 disable_irq(data->spi->irq);
103 disable_irq_wake(data->spi->irq);
105 dev_warn(&data->spi->dev, "current shutdown = %d, old = %d\n",
106 enable, data->shut_down);
111 * This function is the first one which communicates with the mcu so it is
112 * possible that the first attempt will fail
114 static int ssp_check_fwbl(struct ssp_data *data)
118 while (retries++ < 5) {
119 data->cur_firm_rev = ssp_get_firmware_rev(data);
120 if (data->cur_firm_rev == SSP_INVALID_REVISION ||
121 data->cur_firm_rev == SSP_INVALID_REVISION2) {
122 dev_warn(&data->spi->dev,
123 "Invalid revision, trying %d time\n", retries);
129 if (data->cur_firm_rev == SSP_INVALID_REVISION ||
130 data->cur_firm_rev == SSP_INVALID_REVISION2) {
131 dev_err(&data->spi->dev, "SSP_INVALID_REVISION\n");
132 return SSP_FW_DL_STATE_NEED_TO_SCHEDULE;
135 dev_info(&data->spi->dev,
136 "MCU Firm Rev : Old = %8u, New = %8u\n",
138 data->sensorhub_info->fw_rev);
140 if (data->cur_firm_rev != data->sensorhub_info->fw_rev)
141 return SSP_FW_DL_STATE_NEED_TO_SCHEDULE;
143 return SSP_FW_DL_STATE_NONE;
146 static void ssp_reset_mcu(struct ssp_data *data)
148 ssp_enable_mcu(data, false);
149 ssp_clean_pending_list(data);
150 ssp_toggle_mcu_reset_gpio(data);
151 ssp_enable_mcu(data, true);
154 static void ssp_wdt_work_func(struct work_struct *work)
156 struct ssp_data *data = container_of(work, struct ssp_data, work_wdt);
158 dev_err(&data->spi->dev, "%s - Sensor state: 0x%x, RC: %u, CC: %u\n",
159 __func__, data->available_sensors, data->reset_cnt,
163 data->com_fail_cnt = 0;
164 data->timeout_cnt = 0;
167 static void ssp_wdt_timer_func(struct timer_list *t)
169 struct ssp_data *data = from_timer(data, t, wdt_timer);
171 switch (data->fw_dl_state) {
172 case SSP_FW_DL_STATE_FAIL:
173 case SSP_FW_DL_STATE_DOWNLOADING:
174 case SSP_FW_DL_STATE_SYNC:
178 if (data->timeout_cnt > SSP_LIMIT_TIMEOUT_CNT ||
179 data->com_fail_cnt > SSP_LIMIT_RESET_CNT)
180 queue_work(system_power_efficient_wq, &data->work_wdt);
182 mod_timer(&data->wdt_timer, jiffies + msecs_to_jiffies(SSP_WDT_TIME));
185 static void ssp_enable_wdt_timer(struct ssp_data *data)
187 mod_timer(&data->wdt_timer, jiffies + msecs_to_jiffies(SSP_WDT_TIME));
190 static void ssp_disable_wdt_timer(struct ssp_data *data)
192 del_timer_sync(&data->wdt_timer);
193 cancel_work_sync(&data->work_wdt);
197 * ssp_get_sensor_delay() - gets sensor data acquisition period
198 * @data: sensorhub structure
199 * @type: SSP sensor type
201 * Returns acquisition period in ms
203 u32 ssp_get_sensor_delay(struct ssp_data *data, enum ssp_sensor_type type)
205 return data->delay_buf[type];
207 EXPORT_SYMBOL(ssp_get_sensor_delay);
210 * ssp_enable_sensor() - enables data acquisition for sensor
211 * @data: sensorhub structure
212 * @type: SSP sensor type
213 * @delay: delay in ms
215 * Returns 0 or negative value in case of error
217 int ssp_enable_sensor(struct ssp_data *data, enum ssp_sensor_type type,
221 struct ssp_instruction to_send;
223 to_send.a = cpu_to_le32(delay);
224 to_send.b = cpu_to_le32(data->batch_latency_buf[type]);
225 to_send.c = data->batch_opt_buf[type];
227 switch (data->check_status[type]) {
228 case SSP_INITIALIZATION_STATE:
229 /* do calibration step, now just enable */
230 case SSP_ADD_SENSOR_STATE:
231 ret = ssp_send_instruction(data,
232 SSP_MSG2SSP_INST_BYPASS_SENSOR_ADD,
234 (u8 *)&to_send, sizeof(to_send));
236 dev_err(&data->spi->dev, "Enabling sensor failed\n");
237 data->check_status[type] = SSP_NO_SENSOR_STATE;
241 data->sensor_enable |= BIT(type);
242 data->check_status[type] = SSP_RUNNING_SENSOR_STATE;
244 case SSP_RUNNING_SENSOR_STATE:
245 ret = ssp_send_instruction(data,
246 SSP_MSG2SSP_INST_CHANGE_DELAY, type,
247 (u8 *)&to_send, sizeof(to_send));
249 dev_err(&data->spi->dev,
250 "Changing sensor delay failed\n");
255 data->check_status[type] = SSP_ADD_SENSOR_STATE;
259 data->delay_buf[type] = delay;
261 if (atomic_inc_return(&data->enable_refcount) == 1)
262 ssp_enable_wdt_timer(data);
269 EXPORT_SYMBOL(ssp_enable_sensor);
272 * ssp_change_delay() - changes data acquisition for sensor
273 * @data: sensorhub structure
274 * @type: SSP sensor type
275 * @delay: delay in ms
277 * Returns 0 or negative value in case of error
279 int ssp_change_delay(struct ssp_data *data, enum ssp_sensor_type type,
283 struct ssp_instruction to_send;
285 to_send.a = cpu_to_le32(delay);
286 to_send.b = cpu_to_le32(data->batch_latency_buf[type]);
287 to_send.c = data->batch_opt_buf[type];
289 ret = ssp_send_instruction(data, SSP_MSG2SSP_INST_CHANGE_DELAY, type,
290 (u8 *)&to_send, sizeof(to_send));
292 dev_err(&data->spi->dev, "Changing sensor delay failed\n");
296 data->delay_buf[type] = delay;
300 EXPORT_SYMBOL(ssp_change_delay);
303 * ssp_disable_sensor() - disables sensor
305 * @data: sensorhub structure
306 * @type: SSP sensor type
308 * Returns 0 or negative value in case of error
310 int ssp_disable_sensor(struct ssp_data *data, enum ssp_sensor_type type)
315 if (data->sensor_enable & BIT(type)) {
316 command = cpu_to_le32(data->delay_buf[type]);
318 ret = ssp_send_instruction(data,
319 SSP_MSG2SSP_INST_BYPASS_SENSOR_RM,
320 type, (u8 *)&command,
323 dev_err(&data->spi->dev, "Remove sensor fail\n");
327 data->sensor_enable &= ~BIT(type);
330 data->check_status[type] = SSP_ADD_SENSOR_STATE;
332 if (atomic_dec_and_test(&data->enable_refcount))
333 ssp_disable_wdt_timer(data);
337 EXPORT_SYMBOL(ssp_disable_sensor);
339 static irqreturn_t ssp_irq_thread_fn(int irq, void *dev_id)
341 struct ssp_data *data = dev_id;
344 * This wrapper is done to preserve error path for ssp_irq_msg, also
345 * it is defined in different file.
352 static int ssp_initialize_mcu(struct ssp_data *data)
356 ssp_clean_pending_list(data);
358 ret = ssp_get_chipid(data);
359 if (ret != SSP_DEVICE_ID) {
360 dev_err(&data->spi->dev, "%s - MCU %s ret = %d\n", __func__,
361 ret < 0 ? "is not working" : "identification failed",
363 return ret < 0 ? ret : -ENODEV;
366 dev_info(&data->spi->dev, "MCU device ID = %d\n", ret);
369 * needs clarification, for now do not want to export all transfer
370 * methods to sensors' drivers
372 ret = ssp_set_magnetic_matrix(data);
374 dev_err(&data->spi->dev,
375 "%s - ssp_set_magnetic_matrix failed\n", __func__);
379 data->available_sensors = ssp_get_sensor_scanning_info(data);
380 if (data->available_sensors == 0) {
381 dev_err(&data->spi->dev,
382 "%s - ssp_get_sensor_scanning_info failed\n", __func__);
386 data->cur_firm_rev = ssp_get_firmware_rev(data);
387 dev_info(&data->spi->dev, "MCU Firm Rev : New = %8u\n",
390 return ssp_command(data, SSP_MSG2SSP_AP_MCU_DUMP_CHECK, 0);
394 * sensorhub can request its reinitialization as some brutal and rare error
395 * handling. It can be requested from the MCU.
397 static void ssp_refresh_task(struct work_struct *work)
399 struct ssp_data *data = container_of((struct delayed_work *)work,
400 struct ssp_data, work_refresh);
402 dev_info(&data->spi->dev, "refreshing\n");
406 if (ssp_initialize_mcu(data) >= 0) {
407 ssp_sync_available_sensors(data);
408 if (data->last_ap_state != 0)
409 ssp_command(data, data->last_ap_state, 0);
411 if (data->last_resume_state != 0)
412 ssp_command(data, data->last_resume_state, 0);
414 data->timeout_cnt = 0;
415 data->com_fail_cnt = 0;
419 int ssp_queue_ssp_refresh_task(struct ssp_data *data, unsigned int delay)
421 cancel_delayed_work_sync(&data->work_refresh);
423 return queue_delayed_work(system_power_efficient_wq,
425 msecs_to_jiffies(delay));
429 static const struct of_device_id ssp_of_match[] = {
431 .compatible = "samsung,sensorhub-rinato",
432 .data = &ssp_rinato_info,
434 .compatible = "samsung,sensorhub-thermostat",
435 .data = &ssp_thermostat_info,
439 MODULE_DEVICE_TABLE(of, ssp_of_match);
441 static struct ssp_data *ssp_parse_dt(struct device *dev)
443 struct ssp_data *data;
444 struct device_node *node = dev->of_node;
445 const struct of_device_id *match;
447 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
451 data->mcu_ap_gpiod = devm_gpiod_get(dev, "mcu-ap", GPIOD_IN);
452 if (IS_ERR(data->mcu_ap_gpiod))
455 data->ap_mcu_gpiod = devm_gpiod_get(dev, "ap-mcu", GPIOD_OUT_HIGH);
456 if (IS_ERR(data->ap_mcu_gpiod))
459 data->mcu_reset_gpiod = devm_gpiod_get(dev, "mcu-reset",
461 if (IS_ERR(data->mcu_reset_gpiod))
464 match = of_match_node(ssp_of_match, node);
468 data->sensorhub_info = match->data;
470 dev_set_drvdata(dev, data);
475 static struct ssp_data *ssp_parse_dt(struct device *pdev)
482 * ssp_register_consumer() - registers iio consumer in ssp framework
484 * @indio_dev: consumer iio device
485 * @type: ssp sensor type
487 void ssp_register_consumer(struct iio_dev *indio_dev, enum ssp_sensor_type type)
489 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
491 data->sensor_devs[type] = indio_dev;
493 EXPORT_SYMBOL(ssp_register_consumer);
495 static int ssp_probe(struct spi_device *spi)
498 struct ssp_data *data;
500 data = ssp_parse_dt(&spi->dev);
502 dev_err(&spi->dev, "Failed to find platform data\n");
506 ret = mfd_add_devices(&spi->dev, -1, sensorhub_sensor_devs,
507 ARRAY_SIZE(sensorhub_sensor_devs), NULL, 0, NULL);
509 dev_err(&spi->dev, "mfd add devices fail\n");
513 spi->mode = SPI_MODE_1;
514 ret = spi_setup(spi);
516 dev_err(&spi->dev, "Failed to setup spi\n");
520 data->fw_dl_state = SSP_FW_DL_STATE_NONE;
522 spi_set_drvdata(spi, data);
524 mutex_init(&data->comm_lock);
526 for (i = 0; i < SSP_SENSOR_MAX; ++i) {
527 data->delay_buf[i] = SSP_DEFAULT_POLLING_DELAY;
528 data->batch_latency_buf[i] = 0;
529 data->batch_opt_buf[i] = 0;
530 data->check_status[i] = SSP_INITIALIZATION_STATE;
533 data->delay_buf[SSP_BIO_HRM_LIB] = 100;
535 data->time_syncing = true;
537 mutex_init(&data->pending_lock);
538 INIT_LIST_HEAD(&data->pending_list);
540 atomic_set(&data->enable_refcount, 0);
542 INIT_WORK(&data->work_wdt, ssp_wdt_work_func);
543 INIT_DELAYED_WORK(&data->work_refresh, ssp_refresh_task);
545 timer_setup(&data->wdt_timer, ssp_wdt_timer_func, 0);
547 ret = request_threaded_irq(data->spi->irq, NULL,
549 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
552 dev_err(&spi->dev, "Irq request fail\n");
556 /* Let's start with enabled one so irq balance could be ok */
557 data->shut_down = false;
559 /* just to avoid unbalanced irq set wake up */
560 enable_irq_wake(data->spi->irq);
562 data->fw_dl_state = ssp_check_fwbl(data);
563 if (data->fw_dl_state == SSP_FW_DL_STATE_NONE) {
564 ret = ssp_initialize_mcu(data);
566 dev_err(&spi->dev, "Initialize_mcu failed\n");
570 dev_err(&spi->dev, "Firmware version not supported\n");
578 free_irq(data->spi->irq, data);
580 mutex_destroy(&data->pending_lock);
581 mutex_destroy(&data->comm_lock);
583 dev_err(&spi->dev, "Probe failed!\n");
588 static int ssp_remove(struct spi_device *spi)
590 struct ssp_data *data = spi_get_drvdata(spi);
592 if (ssp_command(data, SSP_MSG2SSP_AP_STATUS_SHUTDOWN, 0) < 0)
593 dev_err(&data->spi->dev,
594 "SSP_MSG2SSP_AP_STATUS_SHUTDOWN failed\n");
596 ssp_enable_mcu(data, false);
597 ssp_disable_wdt_timer(data);
599 ssp_clean_pending_list(data);
601 free_irq(data->spi->irq, data);
603 del_timer_sync(&data->wdt_timer);
604 cancel_work_sync(&data->work_wdt);
606 mutex_destroy(&data->comm_lock);
607 mutex_destroy(&data->pending_lock);
609 mfd_remove_devices(&spi->dev);
614 #ifdef CONFIG_PM_SLEEP
615 static int ssp_suspend(struct device *dev)
618 struct ssp_data *data = spi_get_drvdata(to_spi_device(dev));
620 data->last_resume_state = SSP_MSG2SSP_AP_STATUS_SUSPEND;
622 if (atomic_read(&data->enable_refcount) > 0)
623 ssp_disable_wdt_timer(data);
625 ret = ssp_command(data, SSP_MSG2SSP_AP_STATUS_SUSPEND, 0);
627 dev_err(&data->spi->dev,
628 "%s SSP_MSG2SSP_AP_STATUS_SUSPEND failed\n", __func__);
630 ssp_enable_wdt_timer(data);
634 data->time_syncing = false;
635 disable_irq(data->spi->irq);
640 static int ssp_resume(struct device *dev)
643 struct ssp_data *data = spi_get_drvdata(to_spi_device(dev));
645 enable_irq(data->spi->irq);
647 if (atomic_read(&data->enable_refcount) > 0)
648 ssp_enable_wdt_timer(data);
650 ret = ssp_command(data, SSP_MSG2SSP_AP_STATUS_RESUME, 0);
652 dev_err(&data->spi->dev,
653 "%s SSP_MSG2SSP_AP_STATUS_RESUME failed\n", __func__);
654 ssp_disable_wdt_timer(data);
658 /* timesyncing is set by MCU */
659 data->last_resume_state = SSP_MSG2SSP_AP_STATUS_RESUME;
663 #endif /* CONFIG_PM_SLEEP */
665 static const struct dev_pm_ops ssp_pm_ops = {
666 SET_SYSTEM_SLEEP_PM_OPS(ssp_suspend, ssp_resume)
669 static struct spi_driver ssp_driver = {
671 .remove = ssp_remove,
674 .of_match_table = of_match_ptr(ssp_of_match),
679 module_spi_driver(ssp_driver);
681 MODULE_DESCRIPTION("ssp sensorhub driver");
682 MODULE_AUTHOR("Samsung Electronics");
683 MODULE_LICENSE("GPL");