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/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
16 #define SSP_WDT_TIME 10000
17 #define SSP_LIMIT_RESET_CNT 20
18 #define SSP_LIMIT_TIMEOUT_CNT 3
20 /* It is possible that it is max clk rate for version 1.0 of bootcode */
21 #define SSP_BOOT_SPI_HZ 400000
24 * These fields can look enigmatic but this structure is used mainly to flat
25 * some values and depends on command type.
27 struct ssp_instruction
{
31 } __attribute__((__packed__
));
33 static const u8 ssp_magnitude_table
[] = {110, 85, 171, 71, 203, 195, 0, 67,
34 208, 56, 175, 244, 206, 213, 0, 92, 250, 0, 55, 48, 189, 252, 171,
37 static const struct ssp_sensorhub_info ssp_rinato_info
= {
38 .fw_name
= "ssp_B2.fw",
39 .fw_crashed_name
= "ssp_crashed.fw",
41 .mag_table
= ssp_magnitude_table
,
42 .mag_length
= ARRAY_SIZE(ssp_magnitude_table
),
45 static const struct ssp_sensorhub_info ssp_thermostat_info
= {
46 .fw_name
= "thermostat_B2.fw",
47 .fw_crashed_name
= "ssp_crashed.fw",
49 .mag_table
= ssp_magnitude_table
,
50 .mag_length
= ARRAY_SIZE(ssp_magnitude_table
),
53 static const struct mfd_cell sensorhub_sensor_devs
[] = {
55 .name
= "ssp-accelerometer",
58 .name
= "ssp-gyroscope",
62 static void ssp_toggle_mcu_reset_gpio(struct ssp_data
*data
)
64 gpiod_set_value(data
->mcu_reset_gpiod
, 0);
65 usleep_range(1000, 1200);
66 gpiod_set_value(data
->mcu_reset_gpiod
, 1);
70 static void ssp_sync_available_sensors(struct ssp_data
*data
)
74 for (i
= 0; i
< SSP_SENSOR_MAX
; ++i
) {
75 if (data
->available_sensors
& BIT(i
)) {
76 ret
= ssp_enable_sensor(data
, i
, data
->delay_buf
[i
]);
78 dev_err(&data
->spi
->dev
,
79 "Sync sensor nr: %d fail\n", i
);
85 ret
= ssp_command(data
, SSP_MSG2SSP_AP_MCU_SET_DUMPMODE
,
88 dev_err(&data
->spi
->dev
,
89 "SSP_MSG2SSP_AP_MCU_SET_DUMPMODE failed\n");
92 static void ssp_enable_mcu(struct ssp_data
*data
, bool enable
)
94 dev_info(&data
->spi
->dev
, "current shutdown = %d, old = %d\n", enable
,
97 if (enable
&& data
->shut_down
) {
98 data
->shut_down
= false;
99 enable_irq(data
->spi
->irq
);
100 enable_irq_wake(data
->spi
->irq
);
101 } else if (!enable
&& !data
->shut_down
) {
102 data
->shut_down
= true;
103 disable_irq(data
->spi
->irq
);
104 disable_irq_wake(data
->spi
->irq
);
106 dev_warn(&data
->spi
->dev
, "current shutdown = %d, old = %d\n",
107 enable
, data
->shut_down
);
112 * This function is the first one which communicates with the mcu so it is
113 * possible that the first attempt will fail
115 static int ssp_check_fwbl(struct ssp_data
*data
)
119 while (retries
++ < 5) {
120 data
->cur_firm_rev
= ssp_get_firmware_rev(data
);
121 if (data
->cur_firm_rev
== SSP_INVALID_REVISION
||
122 data
->cur_firm_rev
== SSP_INVALID_REVISION2
) {
123 dev_warn(&data
->spi
->dev
,
124 "Invalid revision, trying %d time\n", retries
);
130 if (data
->cur_firm_rev
== SSP_INVALID_REVISION
||
131 data
->cur_firm_rev
== SSP_INVALID_REVISION2
) {
132 dev_err(&data
->spi
->dev
, "SSP_INVALID_REVISION\n");
133 return SSP_FW_DL_STATE_NEED_TO_SCHEDULE
;
136 dev_info(&data
->spi
->dev
,
137 "MCU Firm Rev : Old = %8u, New = %8u\n",
139 data
->sensorhub_info
->fw_rev
);
141 if (data
->cur_firm_rev
!= data
->sensorhub_info
->fw_rev
)
142 return SSP_FW_DL_STATE_NEED_TO_SCHEDULE
;
144 return SSP_FW_DL_STATE_NONE
;
147 static void ssp_reset_mcu(struct ssp_data
*data
)
149 ssp_enable_mcu(data
, false);
150 ssp_clean_pending_list(data
);
151 ssp_toggle_mcu_reset_gpio(data
);
152 ssp_enable_mcu(data
, true);
155 static void ssp_wdt_work_func(struct work_struct
*work
)
157 struct ssp_data
*data
= container_of(work
, struct ssp_data
, work_wdt
);
159 dev_err(&data
->spi
->dev
, "%s - Sensor state: 0x%x, RC: %u, CC: %u\n",
160 __func__
, data
->available_sensors
, data
->reset_cnt
,
164 data
->com_fail_cnt
= 0;
165 data
->timeout_cnt
= 0;
168 static void ssp_wdt_timer_func(struct timer_list
*t
)
170 struct ssp_data
*data
= from_timer(data
, t
, wdt_timer
);
172 switch (data
->fw_dl_state
) {
173 case SSP_FW_DL_STATE_FAIL
:
174 case SSP_FW_DL_STATE_DOWNLOADING
:
175 case SSP_FW_DL_STATE_SYNC
:
179 if (data
->timeout_cnt
> SSP_LIMIT_TIMEOUT_CNT
||
180 data
->com_fail_cnt
> SSP_LIMIT_RESET_CNT
)
181 queue_work(system_power_efficient_wq
, &data
->work_wdt
);
183 mod_timer(&data
->wdt_timer
, jiffies
+ msecs_to_jiffies(SSP_WDT_TIME
));
186 static void ssp_enable_wdt_timer(struct ssp_data
*data
)
188 mod_timer(&data
->wdt_timer
, jiffies
+ msecs_to_jiffies(SSP_WDT_TIME
));
191 static void ssp_disable_wdt_timer(struct ssp_data
*data
)
193 del_timer_sync(&data
->wdt_timer
);
194 cancel_work_sync(&data
->work_wdt
);
198 * ssp_get_sensor_delay() - gets sensor data acquisition period
199 * @data: sensorhub structure
200 * @type: SSP sensor type
202 * Returns acquisition period in ms
204 u32
ssp_get_sensor_delay(struct ssp_data
*data
, enum ssp_sensor_type type
)
206 return data
->delay_buf
[type
];
208 EXPORT_SYMBOL_NS(ssp_get_sensor_delay
, "IIO_SSP_SENSORS");
211 * ssp_enable_sensor() - enables data acquisition for sensor
212 * @data: sensorhub structure
213 * @type: SSP sensor type
214 * @delay: delay in ms
216 * Returns 0 or negative value in case of error
218 int ssp_enable_sensor(struct ssp_data
*data
, enum ssp_sensor_type type
,
222 struct ssp_instruction to_send
;
224 to_send
.a
= cpu_to_le32(delay
);
225 to_send
.b
= cpu_to_le32(data
->batch_latency_buf
[type
]);
226 to_send
.c
= data
->batch_opt_buf
[type
];
228 switch (data
->check_status
[type
]) {
229 case SSP_INITIALIZATION_STATE
:
230 /* do calibration step, now just enable */
231 case SSP_ADD_SENSOR_STATE
:
232 ret
= ssp_send_instruction(data
,
233 SSP_MSG2SSP_INST_BYPASS_SENSOR_ADD
,
235 (u8
*)&to_send
, sizeof(to_send
));
237 dev_err(&data
->spi
->dev
, "Enabling sensor failed\n");
238 data
->check_status
[type
] = SSP_NO_SENSOR_STATE
;
242 data
->sensor_enable
|= BIT(type
);
243 data
->check_status
[type
] = SSP_RUNNING_SENSOR_STATE
;
245 case SSP_RUNNING_SENSOR_STATE
:
246 ret
= ssp_send_instruction(data
,
247 SSP_MSG2SSP_INST_CHANGE_DELAY
, type
,
248 (u8
*)&to_send
, sizeof(to_send
));
250 dev_err(&data
->spi
->dev
,
251 "Changing sensor delay failed\n");
256 data
->check_status
[type
] = SSP_ADD_SENSOR_STATE
;
260 data
->delay_buf
[type
] = delay
;
262 if (atomic_inc_return(&data
->enable_refcount
) == 1)
263 ssp_enable_wdt_timer(data
);
270 EXPORT_SYMBOL_NS(ssp_enable_sensor
, "IIO_SSP_SENSORS");
273 * ssp_change_delay() - changes data acquisition for sensor
274 * @data: sensorhub structure
275 * @type: SSP sensor type
276 * @delay: delay in ms
278 * Returns 0 or negative value in case of error
280 int ssp_change_delay(struct ssp_data
*data
, enum ssp_sensor_type type
,
284 struct ssp_instruction to_send
;
286 to_send
.a
= cpu_to_le32(delay
);
287 to_send
.b
= cpu_to_le32(data
->batch_latency_buf
[type
]);
288 to_send
.c
= data
->batch_opt_buf
[type
];
290 ret
= ssp_send_instruction(data
, SSP_MSG2SSP_INST_CHANGE_DELAY
, type
,
291 (u8
*)&to_send
, sizeof(to_send
));
293 dev_err(&data
->spi
->dev
, "Changing sensor delay failed\n");
297 data
->delay_buf
[type
] = delay
;
301 EXPORT_SYMBOL_NS(ssp_change_delay
, "IIO_SSP_SENSORS");
304 * ssp_disable_sensor() - disables sensor
306 * @data: sensorhub structure
307 * @type: SSP sensor type
309 * Returns 0 or negative value in case of error
311 int ssp_disable_sensor(struct ssp_data
*data
, enum ssp_sensor_type type
)
316 if (data
->sensor_enable
& BIT(type
)) {
317 command
= cpu_to_le32(data
->delay_buf
[type
]);
319 ret
= ssp_send_instruction(data
,
320 SSP_MSG2SSP_INST_BYPASS_SENSOR_RM
,
321 type
, (u8
*)&command
,
324 dev_err(&data
->spi
->dev
, "Remove sensor fail\n");
328 data
->sensor_enable
&= ~BIT(type
);
331 data
->check_status
[type
] = SSP_ADD_SENSOR_STATE
;
333 if (atomic_dec_and_test(&data
->enable_refcount
))
334 ssp_disable_wdt_timer(data
);
338 EXPORT_SYMBOL_NS(ssp_disable_sensor
, "IIO_SSP_SENSORS");
340 static irqreturn_t
ssp_irq_thread_fn(int irq
, void *dev_id
)
342 struct ssp_data
*data
= dev_id
;
345 * This wrapper is done to preserve error path for ssp_irq_msg, also
346 * it is defined in different file.
353 static int ssp_initialize_mcu(struct ssp_data
*data
)
357 ssp_clean_pending_list(data
);
359 ret
= ssp_get_chipid(data
);
360 if (ret
!= SSP_DEVICE_ID
) {
361 dev_err(&data
->spi
->dev
, "%s - MCU %s ret = %d\n", __func__
,
362 ret
< 0 ? "is not working" : "identification failed",
364 return ret
< 0 ? ret
: -ENODEV
;
367 dev_info(&data
->spi
->dev
, "MCU device ID = %d\n", ret
);
370 * needs clarification, for now do not want to export all transfer
371 * methods to sensors' drivers
373 ret
= ssp_set_magnetic_matrix(data
);
375 dev_err(&data
->spi
->dev
,
376 "%s - ssp_set_magnetic_matrix failed\n", __func__
);
380 data
->available_sensors
= ssp_get_sensor_scanning_info(data
);
381 if (data
->available_sensors
== 0) {
382 dev_err(&data
->spi
->dev
,
383 "%s - ssp_get_sensor_scanning_info failed\n", __func__
);
387 data
->cur_firm_rev
= ssp_get_firmware_rev(data
);
388 dev_info(&data
->spi
->dev
, "MCU Firm Rev : New = %8u\n",
391 return ssp_command(data
, SSP_MSG2SSP_AP_MCU_DUMP_CHECK
, 0);
395 * sensorhub can request its reinitialization as some brutal and rare error
396 * handling. It can be requested from the MCU.
398 static void ssp_refresh_task(struct work_struct
*work
)
400 struct ssp_data
*data
= container_of((struct delayed_work
*)work
,
401 struct ssp_data
, work_refresh
);
403 dev_info(&data
->spi
->dev
, "refreshing\n");
407 if (ssp_initialize_mcu(data
) >= 0) {
408 ssp_sync_available_sensors(data
);
409 if (data
->last_ap_state
!= 0)
410 ssp_command(data
, data
->last_ap_state
, 0);
412 if (data
->last_resume_state
!= 0)
413 ssp_command(data
, data
->last_resume_state
, 0);
415 data
->timeout_cnt
= 0;
416 data
->com_fail_cnt
= 0;
420 int ssp_queue_ssp_refresh_task(struct ssp_data
*data
, unsigned int delay
)
422 cancel_delayed_work_sync(&data
->work_refresh
);
424 return queue_delayed_work(system_power_efficient_wq
,
426 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
;
445 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
449 data
->mcu_ap_gpiod
= devm_gpiod_get(dev
, "mcu-ap", GPIOD_IN
);
450 if (IS_ERR(data
->mcu_ap_gpiod
))
453 data
->ap_mcu_gpiod
= devm_gpiod_get(dev
, "ap-mcu", GPIOD_OUT_HIGH
);
454 if (IS_ERR(data
->ap_mcu_gpiod
))
457 data
->mcu_reset_gpiod
= devm_gpiod_get(dev
, "mcu-reset",
459 if (IS_ERR(data
->mcu_reset_gpiod
))
462 data
->sensorhub_info
= device_get_match_data(dev
);
464 dev_set_drvdata(dev
, data
);
470 * ssp_register_consumer() - registers iio consumer in ssp framework
472 * @indio_dev: consumer iio device
473 * @type: ssp sensor type
475 void ssp_register_consumer(struct iio_dev
*indio_dev
, enum ssp_sensor_type type
)
477 struct ssp_data
*data
= dev_get_drvdata(indio_dev
->dev
.parent
->parent
);
479 data
->sensor_devs
[type
] = indio_dev
;
481 EXPORT_SYMBOL_NS(ssp_register_consumer
, "IIO_SSP_SENSORS");
483 static int ssp_probe(struct spi_device
*spi
)
486 struct ssp_data
*data
;
488 data
= ssp_parse_dt(&spi
->dev
);
490 dev_err(&spi
->dev
, "Failed to find platform data\n");
494 ret
= mfd_add_devices(&spi
->dev
, PLATFORM_DEVID_NONE
,
495 sensorhub_sensor_devs
,
496 ARRAY_SIZE(sensorhub_sensor_devs
), NULL
, 0, NULL
);
498 dev_err(&spi
->dev
, "mfd add devices fail\n");
502 spi
->mode
= SPI_MODE_1
;
503 ret
= spi_setup(spi
);
505 dev_err(&spi
->dev
, "Failed to setup spi\n");
509 data
->fw_dl_state
= SSP_FW_DL_STATE_NONE
;
511 spi_set_drvdata(spi
, data
);
513 mutex_init(&data
->comm_lock
);
515 for (i
= 0; i
< SSP_SENSOR_MAX
; ++i
) {
516 data
->delay_buf
[i
] = SSP_DEFAULT_POLLING_DELAY
;
517 data
->batch_latency_buf
[i
] = 0;
518 data
->batch_opt_buf
[i
] = 0;
519 data
->check_status
[i
] = SSP_INITIALIZATION_STATE
;
522 data
->delay_buf
[SSP_BIO_HRM_LIB
] = 100;
524 data
->time_syncing
= true;
526 mutex_init(&data
->pending_lock
);
527 INIT_LIST_HEAD(&data
->pending_list
);
529 atomic_set(&data
->enable_refcount
, 0);
531 INIT_WORK(&data
->work_wdt
, ssp_wdt_work_func
);
532 INIT_DELAYED_WORK(&data
->work_refresh
, ssp_refresh_task
);
534 timer_setup(&data
->wdt_timer
, ssp_wdt_timer_func
, 0);
536 ret
= request_threaded_irq(data
->spi
->irq
, NULL
,
538 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
541 dev_err(&spi
->dev
, "Irq request fail\n");
545 /* Let's start with enabled one so irq balance could be ok */
546 data
->shut_down
= false;
548 /* just to avoid unbalanced irq set wake up */
549 enable_irq_wake(data
->spi
->irq
);
551 data
->fw_dl_state
= ssp_check_fwbl(data
);
552 if (data
->fw_dl_state
== SSP_FW_DL_STATE_NONE
) {
553 ret
= ssp_initialize_mcu(data
);
555 dev_err(&spi
->dev
, "Initialize_mcu failed\n");
559 dev_err(&spi
->dev
, "Firmware version not supported\n");
567 free_irq(data
->spi
->irq
, data
);
569 mutex_destroy(&data
->pending_lock
);
570 mutex_destroy(&data
->comm_lock
);
572 dev_err(&spi
->dev
, "Probe failed!\n");
577 static void ssp_remove(struct spi_device
*spi
)
579 struct ssp_data
*data
= spi_get_drvdata(spi
);
581 if (ssp_command(data
, SSP_MSG2SSP_AP_STATUS_SHUTDOWN
, 0) < 0)
582 dev_err(&data
->spi
->dev
,
583 "SSP_MSG2SSP_AP_STATUS_SHUTDOWN failed\n");
585 ssp_enable_mcu(data
, false);
586 ssp_disable_wdt_timer(data
);
588 ssp_clean_pending_list(data
);
590 free_irq(data
->spi
->irq
, data
);
592 del_timer_sync(&data
->wdt_timer
);
593 cancel_work_sync(&data
->work_wdt
);
595 mutex_destroy(&data
->comm_lock
);
596 mutex_destroy(&data
->pending_lock
);
598 mfd_remove_devices(&spi
->dev
);
601 static int ssp_suspend(struct device
*dev
)
604 struct ssp_data
*data
= spi_get_drvdata(to_spi_device(dev
));
606 data
->last_resume_state
= SSP_MSG2SSP_AP_STATUS_SUSPEND
;
608 if (atomic_read(&data
->enable_refcount
) > 0)
609 ssp_disable_wdt_timer(data
);
611 ret
= ssp_command(data
, SSP_MSG2SSP_AP_STATUS_SUSPEND
, 0);
613 dev_err(&data
->spi
->dev
,
614 "%s SSP_MSG2SSP_AP_STATUS_SUSPEND failed\n", __func__
);
616 ssp_enable_wdt_timer(data
);
620 data
->time_syncing
= false;
621 disable_irq(data
->spi
->irq
);
626 static int ssp_resume(struct device
*dev
)
629 struct ssp_data
*data
= spi_get_drvdata(to_spi_device(dev
));
631 enable_irq(data
->spi
->irq
);
633 if (atomic_read(&data
->enable_refcount
) > 0)
634 ssp_enable_wdt_timer(data
);
636 ret
= ssp_command(data
, SSP_MSG2SSP_AP_STATUS_RESUME
, 0);
638 dev_err(&data
->spi
->dev
,
639 "%s SSP_MSG2SSP_AP_STATUS_RESUME failed\n", __func__
);
640 ssp_disable_wdt_timer(data
);
644 /* timesyncing is set by MCU */
645 data
->last_resume_state
= SSP_MSG2SSP_AP_STATUS_RESUME
;
650 static DEFINE_SIMPLE_DEV_PM_OPS(ssp_pm_ops
, ssp_suspend
, ssp_resume
);
652 static struct spi_driver ssp_driver
= {
654 .remove
= ssp_remove
,
656 .pm
= pm_sleep_ptr(&ssp_pm_ops
),
657 .of_match_table
= ssp_of_match
,
662 module_spi_driver(ssp_driver
);
664 MODULE_DESCRIPTION("ssp sensorhub driver");
665 MODULE_AUTHOR("Samsung Electronics");
666 MODULE_LICENSE("GPL");