-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpiommc.c
669 lines (578 loc) · 15.9 KB
/
gpiommc.c
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
666
667
668
669
/*
* Driver an MMC/SD card on a bitbanging GPIO SPI bus.
* This module hooks up the mmc_spi and spi_gpio modules and also
* provides a configfs interface.
*
* Copyright 2008 Michael Buesch <[email protected]>
*
* Licensed under the GNU/GPL. See COPYING for details.
*/
#include <linux/module.h>
#include <linux/mmc/gpiommc.h>
#include <linux/platform_device.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/spi/spi_gpio_old.h>
#include <linux/configfs.h>
#include <linux/gpio.h>
#include <asm/atomic.h>
#define PFX "gpio-mmc: "
struct gpiommc_device {
struct platform_device *pdev;
struct platform_device *spi_pdev;
struct spi_board_info boardinfo;
};
MODULE_DESCRIPTION("GPIO based MMC driver");
MODULE_AUTHOR("Michael Buesch");
MODULE_LICENSE("GPL");
static int gpiommc_boardinfo_setup(struct spi_board_info *bi,
struct spi_master *master,
void *data)
{
struct gpiommc_device *d = data;
struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
/* Bind the SPI master to the MMC-SPI host driver. */
strlcpy(bi->modalias, "mmc_spi", sizeof(bi->modalias));
bi->max_speed_hz = pdata->max_bus_speed;
bi->bus_num = master->bus_num;
bi->mode = pdata->mode;
return 0;
}
static int gpiommc_probe(struct platform_device *pdev)
{
struct gpiommc_platform_data *mmc_pdata = pdev->dev.platform_data;
struct spi_gpio_platform_data spi_pdata;
struct gpiommc_device *d;
int err;
err = -ENXIO;
if (!mmc_pdata)
goto error;
#ifdef CONFIG_MMC_SPI_MODULE
err = request_module("mmc_spi");
if (err) {
printk(KERN_WARNING PFX
"Failed to request mmc_spi module.\n");
}
#endif /* CONFIG_MMC_SPI_MODULE */
/* Allocate the GPIO-MMC device */
err = -ENOMEM;
d = kzalloc(sizeof(*d), GFP_KERNEL);
if (!d)
goto error;
d->pdev = pdev;
/* Create the SPI-GPIO device */
d->spi_pdev = platform_device_alloc(SPI_GPIO_PLATDEV_NAME,
spi_gpio_next_id());
if (!d->spi_pdev)
goto err_free_d;
memset(&spi_pdata, 0, sizeof(spi_pdata));
spi_pdata.pin_clk = mmc_pdata->pins.gpio_clk;
spi_pdata.pin_miso = mmc_pdata->pins.gpio_do;
spi_pdata.pin_mosi = mmc_pdata->pins.gpio_di;
spi_pdata.pin_cs = mmc_pdata->pins.gpio_cs;
spi_pdata.cs_activelow = mmc_pdata->pins.cs_activelow;
spi_pdata.no_spi_delay = mmc_pdata->no_spi_delay;
spi_pdata.boardinfo_setup = gpiommc_boardinfo_setup;
spi_pdata.boardinfo_setup_data = d;
err = platform_device_add_data(d->spi_pdev, &spi_pdata,
sizeof(spi_pdata));
if (err)
goto err_free_pdev;
err = platform_device_add(d->spi_pdev);
if (err)
goto err_free_pdata;
platform_set_drvdata(pdev, d);
printk(KERN_INFO PFX "MMC-Card ATATA\n");
printk(KERN_INFO PFX "MMC-Card \"%s\" "
"attached to GPIO pins di=%u, do=%u, clk=%u, cs=%u, activelow=%u, nospi=%u, max_bus_speed=%u, mode=%u\n",
mmc_pdata->name, mmc_pdata->pins.gpio_di,
mmc_pdata->pins.gpio_do,
mmc_pdata->pins.gpio_clk,
mmc_pdata->pins.gpio_cs,mmc_pdata->pins.cs_activelow, mmc_pdata->no_spi_delay, mmc_pdata->max_bus_speed, mmc_pdata->mode);
return 0;
err_free_pdata:
kfree(d->spi_pdev->dev.platform_data);
d->spi_pdev->dev.platform_data = NULL;
err_free_pdev:
platform_device_put(d->spi_pdev);
err_free_d:
kfree(d);
error:
return err;
}
static int gpiommc_remove(struct platform_device *pdev)
{
struct gpiommc_device *d = platform_get_drvdata(pdev);
struct gpiommc_platform_data *pdata = d->pdev->dev.platform_data;
platform_device_unregister(d->spi_pdev);
printk(KERN_INFO PFX "GPIO based MMC-Card \"%s\" removed\n",
pdata->name);
platform_device_put(d->spi_pdev);
return 0;
}
#ifdef CONFIG_GPIOMMC_CONFIGFS
/* A device that was created through configfs */
struct gpiommc_configfs_device {
struct config_item item;
/* The platform device, after registration. */
struct platform_device *pdev;
/* The configuration */
struct gpiommc_platform_data pdata;
/* Mutex to protect this structure */
struct mutex mutex;
};
#define GPIO_INVALID -1
static inline bool gpiommc_is_registered(struct gpiommc_configfs_device *dev)
{
return (dev->pdev != NULL);
}
static inline struct gpiommc_configfs_device *ci_to_gpiommc(struct config_item *item)
{
return item ? container_of(item, struct gpiommc_configfs_device, item) : NULL;
}
static struct configfs_attribute gpiommc_attr_DI = {
.ca_owner = THIS_MODULE,
.ca_name = "gpio_data_in",
.ca_mode = S_IRUGO | S_IWUSR,
};
static struct configfs_attribute gpiommc_attr_DO = {
.ca_owner = THIS_MODULE,
.ca_name = "gpio_data_out",
.ca_mode = S_IRUGO | S_IWUSR,
};
static struct configfs_attribute gpiommc_attr_CLK = {
.ca_owner = THIS_MODULE,
.ca_name = "gpio_clock",
.ca_mode = S_IRUGO | S_IWUSR,
};
static struct configfs_attribute gpiommc_attr_CS = {
.ca_owner = THIS_MODULE,
.ca_name = "gpio_chipselect",
.ca_mode = S_IRUGO | S_IWUSR,
};
static struct configfs_attribute gpiommc_attr_CS_activelow = {
.ca_owner = THIS_MODULE,
.ca_name = "gpio_chipselect_activelow",
.ca_mode = S_IRUGO | S_IWUSR,
};
static struct configfs_attribute gpiommc_attr_spimode = {
.ca_owner = THIS_MODULE,
.ca_name = "spi_mode",
.ca_mode = S_IRUGO | S_IWUSR,
};
static struct configfs_attribute gpiommc_attr_spidelay = {
.ca_owner = THIS_MODULE,
.ca_name = "spi_delay",
.ca_mode = S_IRUGO | S_IWUSR,
};
static struct configfs_attribute gpiommc_attr_max_bus_speed = {
.ca_owner = THIS_MODULE,
.ca_name = "max_bus_speed",
.ca_mode = S_IRUGO | S_IWUSR,
};
static struct configfs_attribute gpiommc_attr_register = {
.ca_owner = THIS_MODULE,
.ca_name = "register",
.ca_mode = S_IRUGO | S_IWUSR,
};
static struct configfs_attribute *gpiommc_config_attrs[] = {
&gpiommc_attr_DI,
&gpiommc_attr_DO,
&gpiommc_attr_CLK,
&gpiommc_attr_CS,
&gpiommc_attr_CS_activelow,
&gpiommc_attr_spimode,
&gpiommc_attr_spidelay,
&gpiommc_attr_max_bus_speed,
&gpiommc_attr_register,
NULL,
};
static ssize_t gpiommc_config_attr_show(struct config_item *item,
struct configfs_attribute *attr,
char *page)
{
struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
ssize_t count = 0;
unsigned int gpio;
int err = 0;
mutex_lock(&dev->mutex);
if (attr == &gpiommc_attr_DI) {
gpio = dev->pdata.pins.gpio_di;
if (gpio == GPIO_INVALID)
count = snprintf(page, PAGE_SIZE, "not configured\n");
else
count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
goto out;
}
if (attr == &gpiommc_attr_DO) {
gpio = dev->pdata.pins.gpio_do;
if (gpio == GPIO_INVALID)
count = snprintf(page, PAGE_SIZE, "not configured\n");
else
count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
goto out;
}
if (attr == &gpiommc_attr_CLK) {
gpio = dev->pdata.pins.gpio_clk;
if (gpio == GPIO_INVALID)
count = snprintf(page, PAGE_SIZE, "not configured\n");
else
count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
goto out;
}
if (attr == &gpiommc_attr_CS) {
gpio = dev->pdata.pins.gpio_cs;
if (gpio == GPIO_INVALID)
count = snprintf(page, PAGE_SIZE, "not configured\n");
else
count = snprintf(page, PAGE_SIZE, "%u\n", gpio);
goto out;
}
if (attr == &gpiommc_attr_CS_activelow) {
count = snprintf(page, PAGE_SIZE, "%u\n",
dev->pdata.pins.cs_activelow);
goto out;
}
if (attr == &gpiommc_attr_spimode) {
count = snprintf(page, PAGE_SIZE, "%u\n",
dev->pdata.mode);
goto out;
}
if (attr == &gpiommc_attr_spidelay) {
count = snprintf(page, PAGE_SIZE, "%u\n",
!dev->pdata.no_spi_delay);
goto out;
}
if (attr == &gpiommc_attr_max_bus_speed) {
count = snprintf(page, PAGE_SIZE, "%u\n",
dev->pdata.max_bus_speed);
goto out;
}
if (attr == &gpiommc_attr_register) {
count = snprintf(page, PAGE_SIZE, "%u\n",
gpiommc_is_registered(dev));
goto out;
}
WARN_ON(1);
err = -ENOSYS;
out:
mutex_unlock(&dev->mutex);
return err ? err : count;
}
static int gpiommc_do_register(struct gpiommc_configfs_device *dev,
const char *name)
{
int err;
if (gpiommc_is_registered(dev))
return 0;
if (!gpio_is_valid(dev->pdata.pins.gpio_di) ||
!gpio_is_valid(dev->pdata.pins.gpio_do) ||
!gpio_is_valid(dev->pdata.pins.gpio_clk) ||
!gpio_is_valid(dev->pdata.pins.gpio_cs)) {
printk(KERN_ERR PFX
"configfs: Invalid GPIO pin number(s)\n");
return -EINVAL;
}
strlcpy(dev->pdata.name, name,
sizeof(dev->pdata.name));
dev->pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME,
gpiommc_next_id());
if (!dev->pdev)
return -ENOMEM;
err = platform_device_add_data(dev->pdev, &dev->pdata,
sizeof(dev->pdata));
if (err) {
platform_device_put(dev->pdev);
return err;
}
err = platform_device_add(dev->pdev);
if (err) {
platform_device_put(dev->pdev);
return err;
}
return 0;
}
static int gpiommc_initsd(void){
int err;
struct platform_device *pdev;
struct gpiommc_platform_data pdata;
strcpy(pdata.name, "default");
// PINOUT HERE
pdata.pins.gpio_di = 9;
pdata.pins.gpio_do = 13;
pdata.pins.gpio_clk = 8;
pdata.pins.gpio_cs = 0;
pdata.pins.cs_activelow = 1;
pdata.mode = 0;
pdata.no_spi_delay = 0;
pdata.max_bus_speed = 5000000;
printk(KERN_INFO PFX "[DIRRY-HACK] platform_device_alloc...\n");
pdev = platform_device_alloc(GPIOMMC_PLATDEV_NAME, gpiommc_next_id());
if (!pdev){
printk(KERN_INFO PFX "[DIRRY-HACK] ERROR platform_device_alloc\n");
return -ENOMEM;
}
printk(KERN_INFO PFX "[DIRRY-HACK] platform_device_add_data...\n");
err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
if (err) {
printk(KERN_INFO PFX "[DIRRY-HACK] ERROR platform_device_add_data %d\n", err);
platform_device_put(pdev);
return err;
}
printk(KERN_INFO PFX "[DIRRY-HACK] platform_device_add...\n");
err = platform_device_add(pdev);
if (err) {
printk(KERN_INFO PFX "[DIRRY-HACK] ERROR platform_device_add %d\n", err);
platform_device_put(pdev);
return err;
}
printk(KERN_INFO PFX "[DIRRY-HACK] OK! WAIT FOR GPIO-SPI...\n");
return 0;
}
static void gpiommc_do_unregister(struct gpiommc_configfs_device *dev)
{
if (!gpiommc_is_registered(dev))
return;
platform_device_unregister(dev->pdev);
dev->pdev = NULL;
}
static ssize_t gpiommc_config_attr_store(struct config_item *item,
struct configfs_attribute *attr,
const char *page, size_t count)
{
struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
int err = -EINVAL;
unsigned long data;
mutex_lock(&dev->mutex);
if (attr == &gpiommc_attr_register) {
err = kstrtoul(page, 10, &data);
if (err)
goto out;
err = -EINVAL;
if (data == 1)
err = gpiommc_do_register(dev, item->ci_name);
if (data == 0) {
gpiommc_do_unregister(dev);
err = 0;
}
goto out;
}
if (gpiommc_is_registered(dev)) {
/* The rest of the config parameters can only be set
* as long as the device is not registered, yet. */
err = -EBUSY;
goto out;
}
if (attr == &gpiommc_attr_DI) {
err = kstrtoul(page, 10, &data);
if (err)
goto out;
err = -EINVAL;
if (!gpio_is_valid(data))
goto out;
dev->pdata.pins.gpio_di = data;
err = 0;
goto out;
}
if (attr == &gpiommc_attr_DO) {
err = kstrtoul(page, 10, &data);
if (err)
goto out;
err = -EINVAL;
if (!gpio_is_valid(data))
goto out;
dev->pdata.pins.gpio_do = data;
err = 0;
goto out;
}
if (attr == &gpiommc_attr_CLK) {
err = kstrtoul(page, 10, &data);
if (err)
goto out;
err = -EINVAL;
if (!gpio_is_valid(data))
goto out;
dev->pdata.pins.gpio_clk = data;
err = 0;
goto out;
}
if (attr == &gpiommc_attr_CS) {
err = kstrtoul(page, 10, &data);
if (err)
goto out;
err = -EINVAL;
if (!gpio_is_valid(data))
goto out;
dev->pdata.pins.gpio_cs = data;
err = 0;
goto out;
}
if (attr == &gpiommc_attr_CS_activelow) {
err = kstrtoul(page, 10, &data);
if (err)
goto out;
err = -EINVAL;
if (data != 0 && data != 1)
goto out;
dev->pdata.pins.cs_activelow = data;
err = 0;
goto out;
}
if (attr == &gpiommc_attr_spimode) {
err = kstrtoul(page, 10, &data);
if (err)
goto out;
err = -EINVAL;
switch (data) {
case 0:
dev->pdata.mode = SPI_MODE_0;
break;
case 1:
dev->pdata.mode = SPI_MODE_1;
break;
case 2:
dev->pdata.mode = SPI_MODE_2;
break;
case 3:
dev->pdata.mode = SPI_MODE_3;
break;
default:
goto out;
}
err = 0;
goto out;
}
if (attr == &gpiommc_attr_spidelay) {
err = kstrtoul(page, 10, &data);
if (err)
goto out;
err = -EINVAL;
if (data != 0 && data != 1)
goto out;
dev->pdata.no_spi_delay = !data;
err = 0;
goto out;
}
if (attr == &gpiommc_attr_max_bus_speed) {
err = kstrtoul(page, 10, &data);
if (err)
goto out;
err = -EINVAL;
if (data > UINT_MAX)
goto out;
dev->pdata.max_bus_speed = data;
err = 0;
goto out;
}
WARN_ON(1);
err = -ENOSYS;
out:
mutex_unlock(&dev->mutex);
return err ? err : count;
}
static void gpiommc_config_item_release(struct config_item *item)
{
struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
kfree(dev);
}
static struct configfs_item_operations gpiommc_config_item_ops = {
.release = gpiommc_config_item_release,
.show_attribute = gpiommc_config_attr_show,
.store_attribute = gpiommc_config_attr_store,
};
static struct config_item_type gpiommc_dev_ci_type = {
.ct_item_ops = &gpiommc_config_item_ops,
.ct_attrs = gpiommc_config_attrs,
.ct_owner = THIS_MODULE,
};
static struct config_item *gpiommc_make_item(struct config_group *group,
const char *name)
{
struct gpiommc_configfs_device *dev;
if (strlen(name) > GPIOMMC_MAX_NAMELEN) {
printk(KERN_ERR PFX "configfs: device name too long\n");
return NULL;
}
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return NULL;
mutex_init(&dev->mutex);
config_item_init_type_name(&dev->item, name,
&gpiommc_dev_ci_type);
/* Assign default configuration */
dev->pdata.pins.gpio_di = GPIO_INVALID;
dev->pdata.pins.gpio_do = GPIO_INVALID;
dev->pdata.pins.gpio_clk = GPIO_INVALID;
dev->pdata.pins.gpio_cs = GPIO_INVALID;
dev->pdata.pins.cs_activelow = 1;
dev->pdata.mode = SPI_MODE_0;
dev->pdata.no_spi_delay = 0;
dev->pdata.max_bus_speed = 5000000; /* 5 MHz */
return &(dev->item);
}
static void gpiommc_drop_item(struct config_group *group,
struct config_item *item)
{
struct gpiommc_configfs_device *dev = ci_to_gpiommc(item);
gpiommc_do_unregister(dev);
kfree(dev);
}
static struct configfs_group_operations gpiommc_ct_group_ops = {
.make_item = gpiommc_make_item,
.drop_item = gpiommc_drop_item,
};
static struct config_item_type gpiommc_ci_type = {
.ct_group_ops = &gpiommc_ct_group_ops,
.ct_owner = THIS_MODULE,
};
static struct configfs_subsystem gpiommc_subsys = {
.su_group = {
.cg_item = {
.ci_namebuf = GPIOMMC_PLATDEV_NAME,
.ci_type = &gpiommc_ci_type,
},
},
.su_mutex = __MUTEX_INITIALIZER(gpiommc_subsys.su_mutex),
};
#endif /* CONFIG_GPIOMMC_CONFIGFS */
static struct platform_driver gpiommc_plat_driver = {
.probe = gpiommc_probe,
.remove = gpiommc_remove,
.driver = {
.name = GPIOMMC_PLATDEV_NAME,
.owner = THIS_MODULE,
},
};
int gpiommc_next_id(void)
{
static atomic_t counter = ATOMIC_INIT(-1);
return atomic_inc_return(&counter);
}
EXPORT_SYMBOL(gpiommc_next_id);
static int __init gpiommc_modinit(void)
{
int err;
err = platform_driver_register(&gpiommc_plat_driver);
if (err)
return err;
#ifdef CONFIG_GPIOMMC_CONFIGFS
config_group_init(&gpiommc_subsys.su_group);
err = configfs_register_subsystem(&gpiommc_subsys);
if (err) {
platform_driver_unregister(&gpiommc_plat_driver);
return err;
}
#endif /* CONFIG_GPIOMMC_CONFIGFS */
printk(KERN_INFO PFX "[DIRRY-HACK] Init\n");
gpiommc_initsd();
return 0;
}
module_init(gpiommc_modinit);
static void __exit gpiommc_modexit(void)
{
#ifdef CONFIG_GPIOMMC_CONFIGFS
configfs_unregister_subsystem(&gpiommc_subsys);
#endif
platform_driver_unregister(&gpiommc_plat_driver);
}
module_exit(gpiommc_modexit);