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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
*/
#include <linux/types.h>
#include <linux/msm_dsps.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/of_device.h>
#include <asm/arch_timer.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/sysfs.h>
#include <linux/workqueue.h>
#ifdef CONFIG_SUPPORT_SSC_SPU
#include <linux/adsp/ssc_spu.h>
#endif
#include <soc/qcom/subsystem_restart.h>
#if defined(CONFIG_SEC_FACTORY) && defined(CONFIG_SUPPORT_DUAL_6AXIS)
static bool pretest = false;
#endif
#define IMAGE_LOAD_CMD 1
#define IMAGE_UNLOAD_CMD 0
#define SSR_RESET_CMD 1
#define SET_PRETEST_SSR 2
#define CLR_PRETEST_SSR 3
#define SET_DHALL_SSR 4
#define CLR_DHALL_SSR 5
#define SSR_LOAD_SPU 8
#define SSR_FORCE_RESET_CMD 9
#define CLASS_NAME "ssc"
#define DRV_NAME "sensors"
#define DRV_VERSION "2.00"
#ifdef CONFIG_COMPAT
#define DSPS_IOCTL_READ_SLOW_TIMER32 _IOR(DSPS_IOCTL_MAGIC, 3, compat_uint_t)
#endif
#define QTICK_DIV_FACTOR 0x249F
#ifdef CONFIG_SUPPORT_SSC_SPU
#define SPU_FW_UPDATE_CMD 4
#define SPU_FW_UPDATE_CMD_SIZE 9
#define SPU_SUFFIX_SIZE 2
#define SPU_SUFFIX_IDX 12
#endif
struct sns_ssc_control_s {
struct class *dev_class;
dev_t dev_num;
struct device *dev;
struct cdev *cdev;
};
static struct sns_ssc_control_s sns_ctl;
static ssize_t slpi_boot_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count);
static ssize_t slpi_ssr_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count);
#ifdef CONFIG_SUPPORT_SSC_SPU
static ssize_t slpi_cmd_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count);
static ssize_t slpi_cmd_result_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf);
#endif
struct slpi_loader_private {
void *pil_h;
struct kobject *boot_slpi_obj;
struct attribute_group *attr_group;
};
static struct kobj_attribute slpi_boot_attribute =
__ATTR(boot, 0220, NULL, slpi_boot_store);
static struct kobj_attribute slpi_ssr_attribute =
__ATTR(ssr, 0220, NULL, slpi_ssr_store);
#ifdef CONFIG_SUPPORT_SSC_SPU
static struct kobj_attribute slpi_cmd_attribute =
__ATTR(cmd, 0220, NULL, slpi_cmd_store);
static struct kobj_attribute slpi_cmd_result_attribute =
__ATTR(cmd_result, 0440, slpi_cmd_result_show, NULL);
#endif
static struct attribute *attrs[] = {
&slpi_boot_attribute.attr,
&slpi_ssr_attribute.attr,
#ifdef CONFIG_SUPPORT_SSC_SPU
&slpi_cmd_attribute.attr,
&slpi_cmd_result_attribute.attr,
#endif
NULL,
};
static struct platform_device *slpi_private;
static struct work_struct slpi_ldr_work;
static unsigned int ssc_system_rev;
unsigned int ssc_hw_rev(void)
{
pr_info("%s - ssc_hw_rev = %u\n", __func__, ssc_system_rev);
return ssc_system_rev;
}
EXPORT_SYMBOL(ssc_hw_rev);
static void slpi_load_fw(struct work_struct *slpi_ldr_work)
{
struct platform_device *pdev = slpi_private;
struct slpi_loader_private *priv = NULL;
int ret;
const char *firmware_name = NULL;
if (!pdev) {
pr_err("%s: Platform device null\n", __func__);
goto fail;
}
if (!pdev->dev.of_node) {
dev_err(&pdev->dev,
"%s: Device tree information missing\n", __func__);
goto fail;
}
ret = of_property_read_string(pdev->dev.of_node,
"qcom,firmware-name", &firmware_name);
if (ret < 0) {
pr_err("can't get fw name.\n");
goto fail;
}
ret = of_property_read_u32(pdev->dev.of_node,
"qcom,ssc_hw_rev", &ssc_system_rev);
if (ret < 0)
pr_err("can't get ssc_hw_rev.\n");
else
pr_info("%s - ssc_hw_rev = %u\n", __func__, ssc_system_rev);
priv = platform_get_drvdata(pdev);
if (!priv) {
dev_err(&pdev->dev,
" %s: Private data get failed\n", __func__);
goto fail;
}
priv->pil_h = subsystem_get_with_fwname("slpi", firmware_name);
if (IS_ERR(priv->pil_h)) {
dev_err(&pdev->dev, "%s: pil get failed,\n", __func__);
goto fail;
}
dev_dbg(&pdev->dev, "%s: SLPI image is loaded\n", __func__);
return;
fail:
pr_err("%s: SLPI image loading failed\n", __func__);
}
static void slpi_loader_do(struct platform_device *pdev)
{
dev_dbg(&pdev->dev, "%s: scheduling work to load SLPI fw\n", __func__);
schedule_work(&slpi_ldr_work);
}
static void slpi_loader_unload(struct platform_device *pdev)
{
struct slpi_loader_private *priv = NULL;
priv = platform_get_drvdata(pdev);
if (!priv)
return;
if (priv->pil_h) {
dev_dbg(&pdev->dev, "%s: calling subsystem put\n", __func__);
subsystem_put(priv->pil_h);
priv->pil_h = NULL;
}
}
#ifdef CONFIG_SUPPORT_SSC_SPU
int fw_idx = SSC_ORI;
static bool spu_update_done = false;
int ssc_get_fw_idx(void)
{
return fw_idx;
}
EXPORT_SYMBOL(ssc_get_fw_idx);
char cmd_result_buf[20] = {"fw_update,4:NG"};
static ssize_t slpi_cmd_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf,
size_t count)
{
struct subsys_device *sns_dev = NULL;
struct platform_device *pdev = slpi_private;
struct slpi_loader_private *priv = NULL;
char cmd_buf[15] = {0, };
int nval = 0, cmd = 0;
nval = sscanf(buf, "%9s,%d", cmd_buf, &cmd);
pr_info("%s: buf:%s, cmd:%d,%d\n", __func__, cmd_buf, cmd, nval);
if (!strncmp(cmd_buf, "fw_update", SPU_FW_UPDATE_CMD_SIZE)
&& cmd == SPU_FW_UPDATE_CMD) {
priv = platform_get_drvdata(pdev);
if (!priv) {
pr_err("fail, priv NULL");
return count;
}
sns_dev = (struct subsys_device *)priv->pil_h;
if (!sns_dev) {
pr_err("fail, sns_dev NULL");
return count;
} else {
int ret = 0;
pr_info("Start upload SPU firm");
ret = subsystem_set_fwname("slpi", "slpi_spu");
if (ret < 0) {
pr_err("fail, firmware name for PIL (%d)\n", ret);
return count;
}
subsys_set_fssr(sns_dev, true);
}
if (subsystem_restart_dev(sns_dev) != 0) {
pr_err("subsystem_restart_dev failed\n");
return count;
} else {
strncpy(&cmd_result_buf[SPU_SUFFIX_IDX], "OK", SPU_SUFFIX_SIZE);
}
}
return count;
}
static ssize_t slpi_cmd_result_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", cmd_result_buf);
}
#endif
static ssize_t slpi_ssr_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf,
size_t count)
{
int ssr_cmd = 0;
struct subsys_device *sns_dev = NULL;
struct platform_device *pdev = slpi_private;
struct slpi_loader_private *priv = NULL;
pr_debug("%s: going to call slpi_ssr\n", __func__);
if (kstrtoint(buf, 10, &ssr_cmd) < 0)
return -EINVAL;
#if defined(CONFIG_SEC_FACTORY) && defined(CONFIG_SUPPORT_DUAL_6AXIS)
if (ssr_cmd == SET_PRETEST_SSR) {
pr_info("[FACTORY] Set Pretest SSR. slpi will be restarted!!\n");
pretest = true;
} else if (ssr_cmd == CLR_PRETEST_SSR) {
pr_info("[FACTORY] Clear Pretest SSR. return without slpi ssr!!\n");
pretest = false;
return count;
} else if (ssr_cmd == SET_DHALL_SSR) {
pr_info("[FACTORY] SET_DHALL_SSR. slpi will be restarted!!\n");
pretest = true;
} else if (ssr_cmd == CLR_DHALL_SSR) {
pr_info("[FACTORY] CLR_DHALL_SSR. return without slpi ssr!!\n");
pretest = false;
return count;
} else if (ssr_cmd != SSR_RESET_CMD) {
return -EINVAL;
}
#else
if (ssr_cmd != SSR_RESET_CMD &&
ssr_cmd != SSR_FORCE_RESET_CMD &&
ssr_cmd != SSR_LOAD_SPU)
return -EINVAL;
#endif
priv = platform_get_drvdata(pdev);
if (!priv)
return -EINVAL;
sns_dev = (struct subsys_device *)priv->pil_h;
if (!sns_dev)
return -EINVAL;
dev_err(&pdev->dev, "Something went wrong with SLPI, restarting\n");
if (ssr_cmd == SSR_FORCE_RESET_CMD) {
pr_info("Run Force SSR for SS, cmd:%d\n", ssr_cmd);
subsys_set_fssr(sns_dev, true);
}
#ifdef CONFIG_SUPPORT_SSC_SPU
else if (ssr_cmd == SSR_LOAD_SPU) {
int ret = 0;
pr_info("Upload SPU firm, cmd:%d, %d\n",
ssr_cmd, (int)spu_update_done);
if (spu_update_done)
return count;
ret = subsystem_set_fwname("slpi", "slpi_spu");
if (ret < 0) {
pr_err("fail to set firmware name for PIL (%d)\n", ret);
fw_idx = SSC_ORI_AF_SPU_FAIL;
return count;
}
subsys_set_fssr(sns_dev, true);
fw_idx = SSC_SPU;
spu_update_done = true;
}
#endif
/* subsystem_restart_dev has worker queue to handle */
if (subsystem_restart_dev(sns_dev) != 0) {
dev_err(&pdev->dev, "subsystem_restart_dev failed\n");
return -EINVAL;
}
dev_dbg(&pdev->dev, "SLPI restarted\n");
return count;
}
#if defined(CONFIG_SEC_FACTORY) && defined(CONFIG_SUPPORT_DUAL_6AXIS)
bool is_pretest(void)
{
return pretest;
}
EXPORT_SYMBOL(is_pretest);
#endif
#ifdef CONFIG_DSP_SLEEP_RECOVERY
int slpi_ssr(void)
{
struct subsys_device *slpi_dev = NULL;
struct platform_device *pdev = slpi_private;
struct slpi_loader_private *priv = NULL;
int rc;
dev_dbg(&pdev->dev, "%s: going to call slpi ssr\n ", __func__);
priv = platform_get_drvdata(pdev);
if (!priv)
return -EINVAL;
slpi_dev = (struct subsys_device *)priv->pil_h;
if (!slpi_dev)
return -EINVAL;
dev_info(&pdev->dev, "requesting for slpi restart\n");
#ifndef CONFIG_SEC_SLPI_SLEEP_DEBUG
dev_info(&pdev->dev, "Set force slpi ssr regardless of debug level\n");
subsys_set_fssr(slpi_dev, true);
#endif
/* subsystem_restart_dev has worker queue to handle */
rc = subsystem_restart_dev(slpi_dev);
if (rc) {
dev_err(&pdev->dev, "subsystem_restart_dev failed\n");
return rc;
}
dev_info(&pdev->dev, "slpi restarted by intention\n");
return 0;
}
#endif
static ssize_t slpi_boot_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf,
size_t count)
{
int boot = 0;
if (sscanf(buf, "%du", &boot) != 1)
return -EINVAL;
if (boot == IMAGE_LOAD_CMD) {
pr_debug("%s: going to call slpi_loader_do\n", __func__);
slpi_loader_do(slpi_private);
} else if (boot == IMAGE_UNLOAD_CMD) {
pr_debug("%s: going to call slpi_unloader\n", __func__);
slpi_loader_unload(slpi_private);
}
return count;
}
static int slpi_loader_init_sysfs(struct platform_device *pdev)
{
int ret = -EINVAL;
struct slpi_loader_private *priv = NULL;
slpi_private = NULL;
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
if (!priv) {
ret = -ENOMEM;
return ret;
}
platform_set_drvdata(pdev, priv);
priv->pil_h = NULL;
priv->boot_slpi_obj = NULL;
priv->attr_group = devm_kzalloc(&pdev->dev,
sizeof(*(priv->attr_group)),
GFP_KERNEL);
if (!priv->attr_group) {
dev_err(&pdev->dev, "%s: malloc attr_group failed\n",
__func__);
ret = -ENOMEM;
goto error_return;
}
priv->attr_group->attrs = attrs;
priv->boot_slpi_obj = kobject_create_and_add("boot_slpi", kernel_kobj);
if (!priv->boot_slpi_obj) {
dev_err(&pdev->dev, "%s: sysfs create and add failed\n",
__func__);
ret = -ENOMEM;
goto error_return;
}
ret = sysfs_create_group(priv->boot_slpi_obj, priv->attr_group);
if (ret) {
dev_err(&pdev->dev, "%s: sysfs create group failed %d\n",
__func__, ret);
goto error_return;
}
slpi_private = pdev;
return 0;
error_return:
if (priv->boot_slpi_obj) {
kobject_del(priv->boot_slpi_obj);
priv->boot_slpi_obj = NULL;
}
return ret;
}
static int slpi_loader_remove(struct platform_device *pdev)
{
struct slpi_loader_private *priv = NULL;
priv = platform_get_drvdata(pdev);
if (!priv)
return 0;
if (priv->pil_h) {
subsystem_put(priv->pil_h);
priv->pil_h = NULL;
}
if (priv->boot_slpi_obj) {
sysfs_remove_group(priv->boot_slpi_obj, priv->attr_group);
kobject_del(priv->boot_slpi_obj);
priv->boot_slpi_obj = NULL;
}
return 0;
}
/*
* Read virtual QTimer clock ticks and scale down to 32KHz clock as used
* in DSPS
*/
static u32 sns_read_qtimer(void)
{
u64 val;
val = __arch_counter_get_cntvct();
/*
* To convert ticks from 19.2 Mhz clock to 32768 Hz clock:
* x = (value * 32768) / 19200000
* This is same as first left shift the value by 4 bits, i.e. multiply
* by 16, and then divide by 0x249F. The latter is preferable since
* QTimer tick (value) is 56-bit, so (value * 32768) could overflow,
* while (value * 16) will never do
*/
val <<= 4;
do_div(val, QTICK_DIV_FACTOR);
return (u32)val;
}
static int sensors_ssc_open(struct inode *ip, struct file *fp)
{
return 0;
}
static int sensors_ssc_release(struct inode *inode, struct file *file)
{
return 0;
}
static long sensors_ssc_ioctl(struct file *file,
unsigned int cmd, unsigned long arg)
{
int ret = 0;
u32 val = 0;
switch (cmd) {
case DSPS_IOCTL_READ_SLOW_TIMER:
#ifdef CONFIG_COMPAT
case DSPS_IOCTL_READ_SLOW_TIMER32:
#endif
val = sns_read_qtimer();
ret = put_user(val, (u32 __user *) arg);
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
const struct file_operations sensors_ssc_fops = {
.owner = THIS_MODULE,
.open = sensors_ssc_open,
.release = sensors_ssc_release,
#ifdef CONFIG_COMPAT
.compat_ioctl = sensors_ssc_ioctl,
#endif
.unlocked_ioctl = sensors_ssc_ioctl
};
static int sensors_ssc_probe(struct platform_device *pdev)
{
int ret = slpi_loader_init_sysfs(pdev);
if (ret != 0) {
dev_err(&pdev->dev, "%s: Error in initing sysfs\n", __func__);
return ret;
}
sns_ctl.dev_class = class_create(THIS_MODULE, CLASS_NAME);
if (sns_ctl.dev_class == NULL) {
pr_err("%s: class_create fail.\n", __func__);
goto res_err;
}
ret = alloc_chrdev_region(&sns_ctl.dev_num, 0, 1, DRV_NAME);
if (ret) {
pr_err("%s: alloc_chrdev_region fail.\n", __func__);
goto alloc_chrdev_region_err;
}
sns_ctl.dev = device_create(sns_ctl.dev_class, NULL,
sns_ctl.dev_num,
&sns_ctl, DRV_NAME);
if (IS_ERR(sns_ctl.dev)) {
pr_err("%s: device_create fail.\n", __func__);
goto device_create_err;
}
sns_ctl.cdev = cdev_alloc();
if (sns_ctl.cdev == NULL) {
pr_err("%s: cdev_alloc fail.\n", __func__);
goto cdev_alloc_err;
}
cdev_init(sns_ctl.cdev, &sensors_ssc_fops);
sns_ctl.cdev->owner = THIS_MODULE;
ret = cdev_add(sns_ctl.cdev, sns_ctl.dev_num, 1);
if (ret) {
pr_err("%s: cdev_add fail.\n", __func__);
goto cdev_add_err;
}
INIT_WORK(&slpi_ldr_work, slpi_load_fw);
return 0;
cdev_add_err:
kfree(sns_ctl.cdev);
cdev_alloc_err:
device_destroy(sns_ctl.dev_class, sns_ctl.dev_num);
device_create_err:
unregister_chrdev_region(sns_ctl.dev_num, 1);
alloc_chrdev_region_err:
class_destroy(sns_ctl.dev_class);
res_err:
return -ENODEV;
}
static int sensors_ssc_remove(struct platform_device *pdev)
{
slpi_loader_remove(pdev);
cdev_del(sns_ctl.cdev);
kfree(sns_ctl.cdev);
sns_ctl.cdev = NULL;
device_destroy(sns_ctl.dev_class, sns_ctl.dev_num);
unregister_chrdev_region(sns_ctl.dev_num, 1);
class_destroy(sns_ctl.dev_class);
return 0;
}
static const struct of_device_id msm_ssc_sensors_dt_match[] = {
{.compatible = "qcom,msm-ssc-sensors"},
{},
};
MODULE_DEVICE_TABLE(of, msm_ssc_sensors_dt_match);
static struct platform_driver sensors_ssc_driver = {
.driver = {
.name = "sensors-ssc",
.of_match_table = msm_ssc_sensors_dt_match,
},
.probe = sensors_ssc_probe,
.remove = sensors_ssc_remove,
};
static int __init sensors_ssc_init(void)
{
int rc;
pr_debug("%s driver version %s.\n", DRV_NAME, DRV_VERSION);
rc = platform_driver_register(&sensors_ssc_driver);
if (rc) {
pr_err("%s: Failed to register sensors ssc driver\n",
__func__);
return rc;
}
return 0;
}
static void __exit sensors_ssc_exit(void)
{
platform_driver_unregister(&sensors_ssc_driver);
}
module_init(sensors_ssc_init);
module_exit(sensors_ssc_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Sensors SSC driver");
|