summaryrefslogtreecommitdiff
path: root/linux-kernel-modules/brnkl/brnkl.c
blob: 913c30c3ce8aa63f8ad10a580da3d779563b137e (plain)
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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/i2c/pca954x.h>
#include <linux/i2c/sx150x.h>
#include <linux/spi/spi.h>
#include <linux/delay.h>
#include <linux/gpio/driver.h>

#include "lsm6ds3_platform_data.h"
#include "ltc294x-platform-data.h"
#include "bq24190-platform-data.h"

#include "brnkl.h"
#include "iot-slot.h"
#include "led.h"

/*
 *-----------------------------------------------------------------------------
 * Constants
 *-----------------------------------------------------------------------------
 */
#define MANGOH_RED_I2C_SW_BUS_BASE (PRIMARY_I2C_BUS + 0)
#define MANGOH_RED_I2C_BUS_IOT0 (MANGOH_RED_I2C_SW_BUS_BASE + 0)
#define MANGOH_RED_I2C_BUS_BATTERY_CHARGER (MANGOH_RED_I2C_SW_BUS_BASE + 0)
#define MANGOH_RED_I2C_BUS_USB_HUB (MANGOH_RED_I2C_SW_BUS_BASE + 1)
#define MANGOH_RED_I2C_BUS_GPIO_EXPANDER (MANGOH_RED_I2C_SW_BUS_BASE + 2)
#define MANGOH_RED_I2C_BUS_EXP (MANGOH_RED_I2C_SW_BUS_BASE + 3)

/*
 *-----------------------------------------------------------------------------
 * Types
 *-----------------------------------------------------------------------------
 */
enum mangoh_red_board_rev {
  MANGOH_RED_BOARD_REV_DV2,
  MANGOH_RED_BOARD_REV_DV3,
  MANGOH_RED_BOARD_REV_DV5,
};

/*
 *-----------------------------------------------------------------------------
 * Static Function Declarations
 *-----------------------------------------------------------------------------
 */
static void mangoh_red_release(struct device* dev);
static int mangoh_red_probe(struct platform_device* pdev);
static int mangoh_red_remove(struct platform_device* pdev);
static void mangoh_red_led_release(struct device* dev);
#ifdef ENABLE_IOT_SLOT
static void mangoh_red_iot_slot_release(struct device* dev);
static int mangoh_red_iot_slot_request_i2c(struct i2c_adapter** adapter);
static int mangoh_red_iot_slot_release_i2c(struct i2c_adapter** adapter);
static int mangoh_red_iot_slot_request_spi(struct spi_master** spi_master,
                                           int* cs);
static int mangoh_red_iot_slot_release_spi(void);
static int mangoh_red_iot_slot_request_sdio(void);
static int mangoh_red_iot_slot_release_sdio(void);
static int mangoh_red_iot_slot_request_pcm(void);
static int mangoh_red_iot_slot_release_pcm(void);
#endif /* ENABLE_IOT_SLOT */

/*
 *-----------------------------------------------------------------------------
 * Variables
 *-----------------------------------------------------------------------------
 */

static char* revision_dv2 = "dv2";
static char* revision_dv3 = "dv3";
static char* revision_dv5 = "dv5";

static char* revision = "dv5";
module_param(revision, charp, S_IRUGO);
MODULE_PARM_DESC(revision, "mangOH Red board revision");

static struct platform_driver mangoh_red_driver = {
    .probe = mangoh_red_probe,
    .remove = mangoh_red_remove,
    .driver =
        {
            .name = "mangoh red",
            .owner = THIS_MODULE,
            .bus = &platform_bus_type,
        },
};

static struct mangoh_red_platform_data {
  enum mangoh_red_board_rev board_rev;
} mangoh_red_pdata;

static struct mangoh_red_driver_data {
  struct i2c_client* accelerometer;
  struct i2c_client* pressure;
  struct i2c_client* battery_gauge;
  struct i2c_client* battery_charger;
  bool mux_initialized;
  bool iot_slot_registered;
  bool led_registered;
} mangoh_red_driver_data = {
    .mux_initialized = false,
    .iot_slot_registered = false,
    .led_registered = false,
};

static struct platform_device mangoh_red_device = {
    .name = "mangoh red",
    .id = -1,
    .dev =
        {
            .release = mangoh_red_release,
            .platform_data = &mangoh_red_pdata,
        },
};

static struct pca954x_platform_mode mangoh_red_pca954x_adap_modes[] = {
    {.adap_id = MANGOH_RED_I2C_SW_BUS_BASE + 0,
     .deselect_on_exit = 1,
     .class = 0},
    {.adap_id = MANGOH_RED_I2C_SW_BUS_BASE + 1,
     .deselect_on_exit = 1,
     .class = 0},
    {.adap_id = MANGOH_RED_I2C_SW_BUS_BASE + 2,
     .deselect_on_exit = 1,
     .class = 0},
    {.adap_id = MANGOH_RED_I2C_SW_BUS_BASE + 3,
     .deselect_on_exit = 1,
     .class = 0},
};
static struct pca954x_platform_data mangoh_red_pca954x_pdata = {
    mangoh_red_pca954x_adap_modes, ARRAY_SIZE(mangoh_red_pca954x_adap_modes),
};
static const struct i2c_board_info mangoh_red_pca954x_device_info = {
    I2C_BOARD_INFO("pca9546", 0x71), .platform_data = &mangoh_red_pca954x_pdata,
};

static struct sx150x_platform_data mangoh_red_gpio_expander_platform_data = {
    .gpio_base = -1,
    .oscio_is_gpo = false,
    .io_pullup_ena = 0,
    .io_pulldn_ena = 0,
    .io_open_drain_ena = 0,
    .io_polarity = 0,
    .irq_summary = -1,
    .irq_base = -1,
    .reset_during_probe = true,

};
static const struct i2c_board_info mangoh_red_gpio_expander_devinfo = {
    I2C_BOARD_INFO("sx1509q", 0x3e),
    .platform_data = &mangoh_red_gpio_expander_platform_data, .irq = 0,
};

static struct i2c_board_info mangoh_red_bmi160_devinfo = {
    I2C_BOARD_INFO("bmi160", 0x68),
};

static struct lsm6ds3_platform_data mangoh_red_lsm6ds3_platform_data = {
    .drdy_int_pin = 1,
};
static struct i2c_board_info mangoh_red_lsm6ds3_devinfo = {
    I2C_BOARD_INFO("lsm6ds3", 0x6A),
    .platform_data = &mangoh_red_lsm6ds3_platform_data,
};

static struct i2c_board_info mangoh_red_pressure_devinfo = {
    I2C_BOARD_INFO("bme280", 0x76),
};

static struct ltc294x_platform_data mangoh_red_battery_gauge_platform_data = {
	.r_sense = 18,
	.prescaler_exp = 32,
	.name = "LTC2942",
};
static struct i2c_board_info mangoh_red_battery_gauge_devinfo = {
	I2C_BOARD_INFO("ltc2942", 0x64),
	.platform_data = &mangoh_red_battery_gauge_platform_data,
};

static struct i2c_board_info mangoh_red_battery_charger_devinfo = {
    I2C_BOARD_INFO("bq24190", 0x6B),
};

#ifdef ENABLE_IOT_SLOT
static struct iot_slot_platform_data mangoh_red_iot_slot_pdata = {
    .gpio = {CF3_GPIO42, CF3_GPIO13, CF3_GPIO7, CF3_GPIO8},
    .reset_gpio = CF3_GPIO2,
    .card_detect_gpio = CF3_GPIO33,
    .request_i2c = mangoh_red_iot_slot_request_i2c,
    .release_i2c = mangoh_red_iot_slot_release_i2c,
    .request_spi = mangoh_red_iot_slot_request_spi,
    .release_spi = mangoh_red_iot_slot_release_spi,
    .request_sdio = mangoh_red_iot_slot_request_sdio,
    .release_sdio = mangoh_red_iot_slot_release_sdio,
    .request_pcm = mangoh_red_iot_slot_request_pcm,
    .release_pcm = mangoh_red_iot_slot_release_pcm,
};

static struct platform_device mangoh_red_iot_slot = {
    .name = "iot-slot",
    .id = 0, /* Means IoT slot 0 */
    .dev =
        {
            .platform_data = &mangoh_red_iot_slot_pdata,
            .release = mangoh_red_iot_slot_release,
        },
};
#endif /* ENABLE_IOT_SLOT */

static struct led_platform_data mangoh_red_led_pdata = {
    .gpio = -1,
};

static struct platform_device mangoh_red_led = {
    .name = "led",
    .dev =
        {
            .platform_data = &mangoh_red_led_pdata,
            .release = mangoh_red_led_release,
        },
};

static void mangoh_red_release(struct device* dev) {
  /* Nothing alloc'd, so nothign to free */
}

static int mangoh_red_probe(struct platform_device* pdev) {
  int ret = 0;
#ifdef ENABLE_IOT_SLOT
  int sdio_mux_gpio;
  int pcm_mux_gpio;
#endif /* ENABLE_IOT_SLOT */
  struct gpio_chip* gpio_expander;
  struct i2c_board_info* accelerometer_board_info;
  struct i2c_adapter* other_adapter = NULL;
  struct i2c_adapter* main_adapter;

  dev_info(&pdev->dev, "%s(): probe\n", __func__);

  main_adapter = i2c_get_adapter(PRIMARY_I2C_BUS);
  if (!main_adapter) {
    dev_err(&pdev->dev, "Failed to get primary I2C adapter (%d).\n",
            PRIMARY_I2C_BUS);
    ret = -ENODEV;
    goto done;
  }

  /*
   * This is a workaround of questionable validity for USB issues first
   * seen on the mangOH Green.
   */
  msleep(5000);

  platform_set_drvdata(pdev, &mangoh_red_driver_data);

  /* Map the accelerometer */
  dev_dbg(&pdev->dev, "mapping accelerometer\n");
  /*
   * Pins 11 and 12 of the gpio expander are connected to bmi160's INT1
   * and INT2 pins respectively. It does not appear that the bmi160 driver
   * makes use of these interrupt pins.
   */
  accelerometer_board_info =
      mangoh_red_pdata.board_rev == MANGOH_RED_BOARD_REV_DV2
          ? &mangoh_red_lsm6ds3_devinfo
          : &mangoh_red_bmi160_devinfo;
  mangoh_red_driver_data.accelerometer =
      i2c_new_device(main_adapter, accelerometer_board_info);
  if (!mangoh_red_driver_data.accelerometer) {
    dev_err(&pdev->dev, "Accelerometer is missing\n");
    return -ENODEV;
  }

  /* Map the I2C BMP280 pressure sensor */
  dev_dbg(&pdev->dev, "mapping bmp280 pressure sensor\n");
  mangoh_red_driver_data.pressure =
      i2c_new_device(main_adapter, &mangoh_red_pressure_devinfo);
  if (!mangoh_red_driver_data.pressure) {
    dev_err(&pdev->dev, "Pressure sensor is missing\n");
    return -ENODEV;
  }

  /* Map the I2C BQ24296 driver: for now use the BQ24190 driver code */
  dev_dbg(&pdev->dev, "mapping bq24296 driver\n");
  other_adapter = i2c_get_adapter(MANGOH_RED_I2C_BUS_BATTERY_CHARGER);
  if (!other_adapter) {
    dev_err(&pdev->dev, "No I2C bus %d.\n", MANGOH_RED_I2C_BUS_BATTERY_CHARGER);
    ret = -ENODEV;
    goto cleanup;
  }
  mangoh_red_driver_data.battery_charger =
      i2c_new_device(other_adapter, &mangoh_red_battery_charger_devinfo);
  i2c_put_adapter(other_adapter);
  if (!mangoh_red_driver_data.battery_charger) {
    dev_err(&pdev->dev, "battery charger is missing\n");
    ret = -ENODEV;
    goto cleanup;
  }

	if (mangoh_red_pdata.board_rev != MANGOH_RED_BOARD_REV_DV3) {
		/* Map the I2C ltc2942 battery gauge */
		dev_dbg(&pdev->dev, "mapping ltc2942 battery gauge\n");
		other_adapter =
			i2c_get_adapter(MANGOH_RED_I2C_BUS_BATTERY_CHARGER);
		if (!other_adapter) {
			dev_err(&pdev->dev, "No I2C bus %d.\n",
			        MANGOH_RED_I2C_BUS_BATTERY_CHARGER);
			ret = -ENODEV;
			goto cleanup;
		}
		mangoh_red_driver_data.battery_gauge = i2c_new_device(
			other_adapter, &mangoh_red_battery_gauge_devinfo);
		i2c_put_adapter(other_adapter);
		if (!mangoh_red_driver_data.battery_gauge) {
			dev_err(&pdev->dev, "battery gauge is missing\n");
			ret = -ENODEV;
			goto cleanup;
		}
	}
/*
 * TODO:
 * 3503 USB Hub: 0x08
 *    Looks like there is a driver in the wp85 kernel source at
 *    drivers/usb/misc/usb3503.c. I'm not really sure what benefit is
 *    achieved through using this driver.
 */

cleanup:
  i2c_put_adapter(main_adapter);
  if (ret != 0)
    mangoh_red_remove(pdev);
done:
  return ret;
}

static void try_unregister_i2c_device(struct i2c_client* client) {
  if (client != NULL) {
    i2c_unregister_device(client);
    i2c_put_adapter(client->adapter);
  }
}

static int mangoh_red_remove(struct platform_device* pdev) {
  struct mangoh_red_driver_data* dd = platform_get_drvdata(pdev);

  dev_info(&pdev->dev, "Removing mangoh red platform device\n");

#if 0
	if (mangoh_red_pdata.board_rev != MANGOH_RED_BOARD_REV_DV3)
		try_unregister_i2c_device(dd->battery_gauge);
#endif

  try_unregister_i2c_device(dd->battery_charger);
  try_unregister_i2c_device(dd->pressure);
  try_unregister_i2c_device(dd->accelerometer);

  if (dd->led_registered)
    platform_device_unregister(&mangoh_red_led);

  return 0;
}

/* Release function is needed to avoid warning when device is deleted */
#ifdef ENABLE_IOT_SLOT
static void mangoh_red_iot_slot_release(struct device* dev) { /* do nothing */
}
#endif                                                   /* ENABLE_IOT_SLOT */
static void mangoh_red_led_release(struct device* dev) { /* do nothing */
}

#ifdef ENABLE_IOT_SLOT
static int mangoh_red_iot_slot_request_i2c(struct i2c_adapter** adapter) {
  *adapter = i2c_get_adapter(MANGOH_RED_I2C_BUS_IOT0);
  return *adapter != NULL ? 0 : -EINVAL;
}

static int mangoh_red_iot_slot_release_i2c(struct i2c_adapter** adapter) {
  i2c_put_adapter(*adapter);
  *adapter = NULL;
  return 0;
}

static int mangoh_red_iot_slot_request_spi(struct spi_master** spi_master,
                                           int* cs) {
  *spi_master = spi_busnum_to_master(PRIMARY_SPI_BUS);
  *cs = 0;
  if (!*spi_master) {
    return -ENODEV;
  }

  return 0;
}

static int mangoh_red_iot_slot_release_spi(void) {
  return 0; /* Nothing to do */
}

static int mangoh_red_iot_slot_request_sdio(void) {
  return mangoh_red_mux_sdio_select(SDIO_SELECTION_IOT_SLOT);
}

static int mangoh_red_iot_slot_release_sdio(void) {
  return mangoh_red_mux_sdio_release(SDIO_SELECTION_IOT_SLOT);
}

static int mangoh_red_iot_slot_request_pcm(void) {
  return mangoh_red_mux_pcm_select(PCM_SELECTION_IOT_SLOT);
}

static int mangoh_red_iot_slot_release_pcm(void) {
  return mangoh_red_mux_pcm_release(PCM_SELECTION_IOT_SLOT);
}
#endif /* ENABLE_IOT_SLOT */

static int __init mangoh_red_init(void) {
  platform_driver_register(&mangoh_red_driver);
  printk(KERN_DEBUG "mangoh: registered platform driver\n");

  if (strcmp(revision, revision_dv2) == 0) {
    mangoh_red_pdata.board_rev = MANGOH_RED_BOARD_REV_DV2;
  } else if (strcmp(revision, revision_dv3) == 0) {
    mangoh_red_pdata.board_rev = MANGOH_RED_BOARD_REV_DV3;
  } else if (strcmp(revision, revision_dv5) == 0) {
    mangoh_red_pdata.board_rev = MANGOH_RED_BOARD_REV_DV5;
  } else {
    pr_err("%s: Unsupported mangOH Red board revision (%s)\n", __func__,
           revision);
    return -ENODEV; /* TODO: better value? */
  }

  if (platform_device_register(&mangoh_red_device)) {
    platform_driver_unregister(&mangoh_red_driver);
    return -ENODEV;
  }

  return 0;
}

static void __exit mangoh_red_exit(void) {
  platform_device_unregister(&mangoh_red_device);
  platform_driver_unregister(&mangoh_red_driver);
}

module_init(mangoh_red_init);
module_exit(mangoh_red_exit);

MODULE_ALIAS(PLATFORM_MODULE_PREFIX "mangoh red");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sierra Wireless");
MODULE_DESCRIPTION("Add devices on mangOH Red hardware board");
MODULE_VERSION("1.0");