| 1 | /* |
| 2 | * Custom GPIO-based W1 driver |
| 3 | * |
| 4 | * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org> |
| 5 | * Copyright (C) 2008 Bifferos <bifferos at yahoo.co.uk> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * --------------------------------------------------------------------------- |
| 12 | * |
| 13 | * The behaviour of this driver can be altered by setting some parameters |
| 14 | * from the insmod command line. |
| 15 | * |
| 16 | * The following parameters are adjustable: |
| 17 | * |
| 18 | * bus0 These four arguments must be arrays of |
| 19 | * bus1 3 unsigned integers as follows: |
| 20 | * bus2 |
| 21 | * bus3 <id>,<pin>,<od> |
| 22 | * |
| 23 | * where: |
| 24 | * |
| 25 | * <id> ID to used as device_id for the corresponding bus (required) |
| 26 | * <sda> GPIO pin ID of data pin (required) |
| 27 | * <od> Pin is configured as open drain. |
| 28 | * |
| 29 | * See include/w1-gpio.h for more information about the parameters. |
| 30 | * |
| 31 | * If this driver is built into the kernel, you can use the following kernel |
| 32 | * command line parameters, with the same values as the corresponding module |
| 33 | * parameters listed above: |
| 34 | * |
| 35 | * w1-gpio-custom.bus0 |
| 36 | * w1-gpio-custom.bus1 |
| 37 | * w1-gpio-custom.bus2 |
| 38 | * w1-gpio-custom.bus3 |
| 39 | */ |
| 40 | |
| 41 | #include <linux/kernel.h> |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/platform_device.h> |
| 45 | |
| 46 | #include <linux/w1-gpio.h> |
| 47 | |
| 48 | #define DRV_NAME "w1-gpio-custom" |
| 49 | #define DRV_DESC "Custom GPIO-based W1 driver" |
| 50 | #define DRV_VERSION "0.1.1" |
| 51 | |
| 52 | #define PFX DRV_NAME ": " |
| 53 | |
| 54 | #define BUS_PARAM_ID 0 |
| 55 | #define BUS_PARAM_PIN 1 |
| 56 | #define BUS_PARAM_OD 2 |
| 57 | |
| 58 | #define BUS_PARAM_REQUIRED 3 |
| 59 | #define BUS_PARAM_COUNT 3 |
| 60 | #define BUS_COUNT_MAX 4 |
| 61 | |
| 62 | static unsigned int bus0[BUS_PARAM_COUNT] __initdata; |
| 63 | static unsigned int bus1[BUS_PARAM_COUNT] __initdata; |
| 64 | static unsigned int bus2[BUS_PARAM_COUNT] __initdata; |
| 65 | static unsigned int bus3[BUS_PARAM_COUNT] __initdata; |
| 66 | |
| 67 | static unsigned int bus_nump[BUS_COUNT_MAX] __initdata; |
| 68 | |
| 69 | #define BUS_PARM_DESC " config -> id,pin,od" |
| 70 | |
| 71 | module_param_array(bus0, uint, &bus_nump[0], 0); |
| 72 | MODULE_PARM_DESC(bus0, "bus0" BUS_PARM_DESC); |
| 73 | module_param_array(bus1, uint, &bus_nump[1], 0); |
| 74 | MODULE_PARM_DESC(bus1, "bus1" BUS_PARM_DESC); |
| 75 | module_param_array(bus2, uint, &bus_nump[2], 0); |
| 76 | MODULE_PARM_DESC(bus2, "bus2" BUS_PARM_DESC); |
| 77 | module_param_array(bus3, uint, &bus_nump[3], 0); |
| 78 | MODULE_PARM_DESC(bus3, "bus3" BUS_PARM_DESC); |
| 79 | |
| 80 | static struct platform_device *devices[BUS_COUNT_MAX]; |
| 81 | static unsigned int nr_devices; |
| 82 | |
| 83 | static void w1_gpio_custom_cleanup(void) |
| 84 | { |
| 85 | int i; |
| 86 | |
| 87 | for (i = 0; i < nr_devices; i++) |
| 88 | if (devices[i]) |
| 89 | platform_device_put(devices[i]); |
| 90 | } |
| 91 | |
| 92 | static int __init w1_gpio_custom_add_one(unsigned int id, unsigned int *params) |
| 93 | { |
| 94 | struct platform_device *pdev; |
| 95 | struct w1_gpio_platform_data pdata; |
| 96 | int err; |
| 97 | |
| 98 | if (!bus_nump[id]) |
| 99 | return 0; |
| 100 | |
| 101 | if (bus_nump[id] < BUS_PARAM_REQUIRED) { |
| 102 | printk(KERN_ERR PFX "not enough parameters for bus%d\n", id); |
| 103 | err = -EINVAL; |
| 104 | goto err; |
| 105 | } |
| 106 | |
| 107 | pdev = platform_device_alloc("w1-gpio", params[BUS_PARAM_ID]); |
| 108 | if (!pdev) { |
| 109 | err = -ENOMEM; |
| 110 | goto err; |
| 111 | } |
| 112 | |
| 113 | pdata.pin = params[BUS_PARAM_PIN]; |
| 114 | pdata.is_open_drain = params[BUS_PARAM_OD] ? 1:0; |
| 115 | |
| 116 | err = platform_device_add_data(pdev, &pdata, sizeof(pdata)); |
| 117 | if (err) |
| 118 | goto err_put; |
| 119 | |
| 120 | err = platform_device_add(pdev); |
| 121 | if (err) |
| 122 | goto err_put; |
| 123 | |
| 124 | devices[nr_devices++] = pdev; |
| 125 | return 0; |
| 126 | |
| 127 | err_put: |
| 128 | platform_device_put(pdev); |
| 129 | err: |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | static int __init w1_gpio_custom_probe(void) |
| 134 | { |
| 135 | int err; |
| 136 | |
| 137 | nr_devices = 0; |
| 138 | printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n"); |
| 139 | |
| 140 | err = w1_gpio_custom_add_one(0, bus0); |
| 141 | if (err) goto err; |
| 142 | |
| 143 | err = w1_gpio_custom_add_one(1, bus1); |
| 144 | if (err) goto err; |
| 145 | |
| 146 | err = w1_gpio_custom_add_one(2, bus2); |
| 147 | if (err) goto err; |
| 148 | |
| 149 | err = w1_gpio_custom_add_one(3, bus3); |
| 150 | if (err) goto err; |
| 151 | |
| 152 | if (!nr_devices) { |
| 153 | printk(KERN_ERR PFX "no bus parameter(s) specified\n"); |
| 154 | err = -ENODEV; |
| 155 | goto err; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | |
| 160 | err: |
| 161 | w1_gpio_custom_cleanup(); |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | #ifdef MODULE |
| 166 | static int __init w1_gpio_custom_init(void) |
| 167 | { |
| 168 | return w1_gpio_custom_probe(); |
| 169 | } |
| 170 | module_init(w1_gpio_custom_init); |
| 171 | |
| 172 | static void __exit w1_gpio_custom_exit(void) |
| 173 | { |
| 174 | w1_gpio_custom_cleanup(); |
| 175 | } |
| 176 | module_exit(w1_gpio_custom_exit); |
| 177 | #else |
| 178 | subsys_initcall(w1_gpio_custom_probe); |
| 179 | #endif /* MODULE*/ |
| 180 | |
| 181 | MODULE_LICENSE("GPL v2"); |
| 182 | MODULE_AUTHOR("Bifferos <bifferos at yahoo.co.uk >"); |
| 183 | MODULE_DESCRIPTION(DRV_DESC); |
| 184 | MODULE_VERSION(DRV_VERSION); |
| 185 | |