1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
/*
* Copyright (C) 2015, Samsung Electronics Co. Ltd. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "ssp_dev.h"
/*
extern int poweroff_charging;
extern int boot_mode_recovery;
*/
void ssp_enable(struct ssp_data *data, bool enable)
{
ssp_infof("enable = %d, old enable = %d",
enable, data->bSspShutdown);
if (enable && data->bSspShutdown) {
data->bSspShutdown = false;
enable_irq(data->iIrq);
enable_irq_wake(data->iIrq);
} else if (!enable && !data->bSspShutdown) {
data->bSspShutdown = true;
disable_irq(data->iIrq);
disable_irq_wake(data->iIrq);
} else
ssp_errf("enable error");
}
/************************************************************************/
/* interrupt happened due to transition/change of SSP MCU */
/************************************************************************/
static irqreturn_t sensordata_irq_thread_fn(int iIrq, void *dev_id)
{
struct ssp_data *data = dev_id;
struct timespec ts;
ts = ktime_to_timespec(ktime_get_boottime());
data->timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
if (gpio_get_value(data->mcu_int1)) {
ssp_info("MCU int HIGH");
return IRQ_HANDLED;
}
select_irq_msg(data);
data->uIrqCnt++;
return IRQ_HANDLED;
}
/*************************************************************************/
/* initialize sensor hub */
/*************************************************************************/
static void initialize_variable(struct ssp_data *data)
{
int iSensorIndex;
for (iSensorIndex = 0; iSensorIndex < SENSOR_MAX; iSensorIndex++) {
data->adDelayBuf[iSensorIndex] = DEFUALT_POLLING_DELAY;
data->aiCheckStatus[iSensorIndex] = INITIALIZATION_STATE;
}
data->uSensorState = NORMAL_SENSOR_STATE_K;
data->uMagCntlRegData = 1;
data->bSspShutdown = true;
data->bTimeSyncing = true;
data->buf[GYROSCOPE_SENSOR].gyro_dps = GYROSCOPE_DPS2000;
data->uIr_Current = DEFUALT_IR_CURRENT;
#if CONFIG_SEC_DEBUG
data->bMcuDumpMode = sec_debug_is_enabled();
#endif
INIT_LIST_HEAD(&data->pending_list);
initialize_function_pointer(data);
}
int initialize_mcu(struct ssp_data *data)
{
int iRet = 0;
clean_pending_list(data);
iRet = get_chipid(data);
ssp_info("MCU device ID = %d, reading ID = %d", DEVICE_ID, iRet);
if (iRet != DEVICE_ID) {
if (iRet < 0) {
ssp_errf("MCU is not working : 0x%x", iRet);
} else {
ssp_errf("MCU identification failed");
iRet = -ENODEV;
}
goto out;
}
iRet = set_sensor_position(data);
if (iRet < 0) {
ssp_errf("set_sensor_position failed");
goto out;
}
#ifdef CONFIG_SENSORS_MULTIPLE_GLASS_TYPE
iRet = set_glass_type(data);
if (iRet < 0) {
pr_err("[SSP]: %s - set_sensor_position failed\n", __func__);
goto out;
}
#endif
data->uSensorState = get_sensor_scanning_info(data);
if (data->uSensorState == 0) {
ssp_errf("get_sensor_scanning_info failed");
iRet = ERROR;
goto out;
}
iRet = initialize_magnetic_sensor(data);
if (iRet < 0)
ssp_errf("initialize magnetic sensor failed");
data->uCurFirmRev = get_firmware_rev(data);
ssp_info("MCU Firm Rev : New = %8u", data->uCurFirmRev);
out:
return iRet;
}
static int initialize_irq(struct ssp_data *data)
{
int iRet, iIrq;
iIrq = gpio_to_irq(data->mcu_int1);
ssp_info("requesting IRQ %d", iIrq);
iRet = request_threaded_irq(iIrq, NULL, sensordata_irq_thread_fn,
IRQF_TRIGGER_FALLING|IRQF_ONESHOT, "SSP_Int", data);
if (iRet < 0) {
ssp_errf("request_irq(%d) failed for gpio %d (%d)",
iIrq, iIrq, iRet);
goto err_request_irq;
}
/* start with interrupts disabled */
data->iIrq = iIrq;
disable_irq(data->iIrq);
return 0;
err_request_irq:
gpio_free(data->mcu_int1);
return iRet;
}
static void work_function_firmware_update(struct work_struct *work)
{
struct ssp_data *data = container_of((struct delayed_work *)work,
struct ssp_data, work_firmware);
int iRet;
ssp_infof();
iRet = forced_to_download_binary(data, KERNEL_BINARY);
if (iRet < 0) {
ssp_infof("forced_to_download_binary failed!");
data->uSensorState = 0;
return;
}
queue_refresh_task(data, SSP_SW_RESET_TIME);
}
static int ssp_parse_dt(struct device *dev,
struct ssp_data *data)
{
struct device_node *np = dev->of_node;
enum of_gpio_flags flags;
int errorno = 0;
/* gpio pins */
data->mcu_int1 = of_get_named_gpio_flags(np, "ssp,mcu_int1-gpio",
0, &flags);
if (data->mcu_int1 < 0) {
errorno = data->mcu_int1;
goto dt_exit;
}
data->mcu_int2 = of_get_named_gpio_flags(np, "ssp,mcu_int2-gpio",
0, &flags);
if (data->mcu_int2 < 0) {
errorno = data->mcu_int2;
goto dt_exit;
}
data->ap_int = of_get_named_gpio_flags(np, "ssp,ap_int-gpio",
0, &flags);
if (data->ap_int < 0) {
errorno = data->ap_int;
goto dt_exit;
}
data->rst = of_get_named_gpio_flags(np, "ssp,rst-gpio",
0, &flags);
if (data->rst < 0) {
errorno = data->rst;
goto dt_exit;
}
/* sensor positions */
if (of_property_read_u32(np, "ssp,acc-position", &data->accel_position))
data->accel_position = 0;
if (of_property_read_u32(np, "ssp,mag-position", &data->mag_position))
data->mag_position = 0;
ssp_info("acc-posi[%d] mag-posi[%d]",
data->accel_position, data->mag_position);
/* prox thresh */
if (of_property_read_u32(np, "ssp,prox-hi_thresh",
&data->uProxHiThresh_default))
data->uProxHiThresh_default = DEFUALT_HIGH_THRESHOLD;
if (of_property_read_u32(np, "ssp,prox-low_thresh",
&data->uProxLoThresh_default))
data->uProxLoThresh_default = DEFUALT_LOW_THRESHOLD;
ssp_info("hi-thresh[%u] low-thresh[%u]",
data->uProxHiThresh_default, data->uProxLoThresh_default);
#ifdef CONFIG_SENSORS_MULTIPLE_GLASS_TYPE
if (of_property_read_u32(np, "ssp-glass-type", &data->glass_type))
data->glass_type = 0;
#endif
/* acc type */
if (of_property_read_u32(np, "ssp-acc-type", &data->acc_type))
data->acc_type = 0;
ssp_info("acc-type = %d", data->acc_type);
/* pressure type */
if (of_property_read_u32(np, "ssp-pressure-type", &data->pressure_type))
data->pressure_type = 0;
ssp_info("pressure-type = %d", data->pressure_type);
/* mag matrix */
if (of_property_read_u8_array(np, "ssp,mag-array",
data->pdc_matrix, sizeof(data->pdc_matrix))) {
ssp_err("no mag-array, set as 0");
}
/* set off gpio pins */
errorno = gpio_request(data->mcu_int1, "mcu_ap_int1");
if (errorno) {
ssp_err("failed to request MCU_INT1 for SSP");
goto dt_exit;
}
errorno = gpio_direction_input(data->mcu_int1);
if (errorno) {
ssp_err("failed to set mcu_int1 as input");
goto dt_exit;
}
errorno = gpio_request(data->mcu_int2, "MCU_INT2");
if (errorno) {
ssp_err("failed to request MCU_INT2 for SSP");
goto dt_exit;
}
gpio_direction_input(data->mcu_int2);
errorno = gpio_request(data->ap_int, "AP_MCU_INT");
if (errorno) {
ssp_err("failed to request AP_INT for SSP");
goto dt_exit;
}
gpio_direction_output(data->ap_int, 1);
errorno = gpio_request(data->rst, "MCU_RST");
if (errorno) {
ssp_err("failed to request MCU_RST for SSP");
goto dt_exit;
}
gpio_direction_output(data->rst, 1);
dt_exit:
return errorno;
}
static int ssp_suspend(struct device *dev)
{
struct spi_device *spi_dev = to_spi_device(dev);
struct ssp_data *data = spi_get_drvdata(spi_dev);
ssp_infof();
data->uLastResumeState = MSG2SSP_AP_STATUS_SUSPEND;
disable_debug_timer(data);
if (SUCCESS != ssp_send_cmd(data, MSG2SSP_AP_STATUS_SUSPEND, 0))
ssp_errf("MSG2SSP_AP_STATUS_SUSPEND failed");
data->bTimeSyncing = false;
disable_irq_nosync(data->iIrq);
return 0;
}
static int ssp_resume(struct device *dev)
{
struct spi_device *spi_dev = to_spi_device(dev);
struct ssp_data *data = spi_get_drvdata(spi_dev);
enable_irq(data->iIrq);
ssp_infof();
enable_debug_timer(data);
if (SUCCESS != ssp_send_cmd(data, MSG2SSP_AP_STATUS_RESUME, 0))
ssp_errf("MSG2SSP_AP_STATUS_RESUME failed");
data->uLastResumeState = MSG2SSP_AP_STATUS_RESUME;
return 0;
}
static const struct dev_pm_ops ssp_pm_ops = {
.suspend = ssp_suspend,
.resume = ssp_resume
};
static int ssp_probe(struct spi_device *spi)
{
int iRet = 0;
struct ssp_data *data;
ssp_infof();
/*
if (poweroff_charging == 1 || boot_mode_recovery == 1) {
ssp_err("probe exit : lpm %d, recovery %d",
poweroff_charging, boot_mode_recovery);
return -ENODEV;
}
*/
data = kzalloc(sizeof(*data), GFP_KERNEL);
if (data == NULL) {
ssp_errf("failed to allocate memory for data");
iRet = -ENOMEM;
goto exit;
}
if (spi->dev.of_node) {
iRet = ssp_parse_dt(&spi->dev, data);
if (iRet) {
ssp_errf("Failed to parse DT");
goto err_setup;
}
} else {
ssp_errf("failed to get device node");
iRet = -ENODEV;
goto err_setup;
}
spi->mode = SPI_MODE_1;
if (spi_setup(spi)) {
ssp_errf("failed to setup spi");
iRet = -ENODEV;
goto err_setup;
}
data->fw_dl_state = FW_DL_STATE_NONE;
data->spi = spi;
spi_set_drvdata(spi, data);
mutex_init(&data->comm_mutex);
mutex_init(&data->pending_mutex);
pr_info("\n#####################################################\n");
INIT_DELAYED_WORK(&data->work_firmware, work_function_firmware_update);
wake_lock_init(&data->ssp_wake_lock,
WAKE_LOCK_SUSPEND, "ssp_wake_lock");
iRet = initialize_indio_dev(data);
if (iRet < 0) {
ssp_errf("could not create input device");
goto err_input_register_device;
}
iRet = initialize_debug_timer(data);
if (iRet < 0) {
ssp_errf("could not create workqueue");
goto err_create_workqueue;
}
iRet = initialize_irq(data);
if (iRet < 0) {
ssp_errf("could not create irq");
goto err_setup_irq;
}
iRet = initialize_sysfs(data);
if (iRet < 0) {
ssp_errf("could not create sysfs");
goto err_sysfs_create;
}
initialize_variable(data);
/* init sensorhub device */
iRet = ssp_sensorhub_initialize(data);
if (iRet < 0) {
ssp_errf("ssp_sensorhub_initialize err(%d)", iRet);
ssp_sensorhub_remove(data);
}
ssp_enable(data, true);
/* check boot loader binary */
data->fw_dl_state = check_fwbl(data);
if (data->fw_dl_state == FW_DL_STATE_NONE) {
iRet = initialize_mcu(data);
if (iRet == ERROR) {
toggle_mcu_reset(data);
} else if (iRet < ERROR) {
ssp_errf("initialize_mcu failed");
goto err_read_reg;
}
}
ssp_infof("probe success!");
enable_debug_timer(data);
if (data->fw_dl_state == FW_DL_STATE_NEED_TO_SCHEDULE) {
ssp_info("Firmware update is scheduled");
schedule_delayed_work(&data->work_firmware,
msecs_to_jiffies(1000));
data->fw_dl_state = FW_DL_STATE_SCHEDULED;
} else if (data->fw_dl_state == FW_DL_STATE_FAIL) {
data->bSspShutdown = true;
}
data->bProbeIsDone = true;
iRet = 0;
goto exit;
err_read_reg:
remove_sysfs(data);
err_sysfs_create:
free_irq(data->iIrq, data);
gpio_free(data->mcu_int1);
err_setup_irq:
destroy_workqueue(data->debug_wq);
err_create_workqueue:
remove_indio_dev(data);
err_input_register_device:
wake_lock_destroy(&data->ssp_wake_lock);
mutex_destroy(&data->comm_mutex);
mutex_destroy(&data->pending_mutex);
err_setup:
kfree(data);
ssp_errf("probe failed!");
exit:
pr_info("#####################################################\n\n");
return iRet;
}
static void ssp_shutdown(struct spi_device *spi_dev)
{
struct ssp_data *data = spi_get_drvdata(spi_dev);
ssp_infof();
if (data->bProbeIsDone == false)
goto exit;
disable_debug_timer(data);
if (data->fw_dl_state >= FW_DL_STATE_SCHEDULED &&
data->fw_dl_state < FW_DL_STATE_DONE) {
ssp_errf("cancel_delayed_work_sync state = %d",
data->fw_dl_state);
cancel_delayed_work_sync(&data->work_firmware);
}
if (SUCCESS != ssp_send_cmd(data, MSG2SSP_AP_STATUS_SHUTDOWN, 0))
ssp_errf("MSG2SSP_AP_STATUS_SHUTDOWN failed");
data->bSspShutdown = true;
disable_irq_nosync(data->iIrq);
disable_irq_wake(data->iIrq);
clean_pending_list(data);
free_irq(data->iIrq, data);
gpio_free(data->mcu_int1);
remove_sysfs(data);
ssp_sensorhub_remove(data);
del_timer_sync(&data->debug_timer);
cancel_work_sync(&data->work_debug);
cancel_delayed_work_sync(&data->work_refresh);
destroy_workqueue(data->debug_wq);
wake_lock_destroy(&data->ssp_wake_lock);
mutex_destroy(&data->comm_mutex);
mutex_destroy(&data->pending_mutex);
toggle_mcu_reset(data);
ssp_infof("done");
exit:
kfree(data);
}
static const struct spi_device_id ssp_id[] = {
{"ssp", 0},
{}
};
MODULE_DEVICE_TABLE(spi, ssp_id);
#ifdef CONFIG_OF
static struct of_device_id ssp_match_table[] = {
{ .compatible = "ssp,STM32F",},
{},
};
#endif
static struct spi_driver ssp_driver = {
.probe = ssp_probe,
.shutdown = ssp_shutdown,
.id_table = ssp_id,
.driver = {
.pm = &ssp_pm_ops,
.owner = THIS_MODULE,
.name = "ssp",
#ifdef CONFIG_OF
.of_match_table = ssp_match_table
#endif
},
};
module_spi_driver(ssp_driver);
MODULE_DESCRIPTION("Seamless Sensor Platform(SSP) dev driver");
MODULE_AUTHOR("Samsung Electronics");
MODULE_LICENSE("GPL");
|