blob: 5a8525d7694d5a6c461cdac6b5134d91c215a128 [file] [log] [blame]
Jeron Susan4a2d59e2016-08-24 15:23:19 +08001/* Copyright (c) 2012-2014, 2016 The Linux Foundation. All rights reserved.
Kenneth Heitkeee44ade2012-02-08 13:45:33 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#ifndef _LINUX_SPMI_H
14#define _LINUX_SPMI_H
15
16#include <linux/types.h>
17#include <linux/device.h>
18#include <linux/mod_devicetable.h>
19
20/* Maximum slave identifier */
21#define SPMI_MAX_SLAVE_ID 16
22
23/* SPMI Commands */
24enum spmi_commands {
25 SPMI_CMD_EXT_WRITE = 0x00,
26 SPMI_CMD_RESET = 0x10,
27 SPMI_CMD_SLEEP = 0x11,
28 SPMI_CMD_SHUTDOWN = 0x12,
29 SPMI_CMD_WAKEUP = 0x13,
30 SPMI_CMD_AUTHENTICATE = 0x14,
31 SPMI_CMD_MSTR_READ = 0x15,
32 SPMI_CMD_MSTR_WRITE = 0x16,
33 SPMI_CMD_TRANSFER_BUS_OWNERSHIP = 0x1A,
34 SPMI_CMD_DDB_MASTER_READ = 0x1B,
35 SPMI_CMD_DDB_SLAVE_READ = 0x1C,
36 SPMI_CMD_EXT_READ = 0x20,
37 SPMI_CMD_EXT_WRITEL = 0x30,
38 SPMI_CMD_EXT_READL = 0x38,
39 SPMI_CMD_WRITE = 0x40,
40 SPMI_CMD_READ = 0x60,
41 SPMI_CMD_ZERO_WRITE = 0x80,
42};
43
44struct spmi_device;
45
46/**
47 * struct spmi_controller: interface to the SPMI master controller
48 * @nr: board-specific number identifier for this controller/bus
49 * @name: name for this controller
50 * @cmd: sends a non-data command sequence on the SPMI bus.
51 * @read_cmd: sends a register read command sequence on the SPMI bus.
52 * @write_cmd: sends a register write command sequence on the SPMI bus.
53 */
54struct spmi_controller {
55 struct device dev;
56 unsigned int nr;
Kenneth Heitke42120dc2013-04-29 16:00:59 -060057 struct completion dev_released;
Kenneth Heitkeee44ade2012-02-08 13:45:33 -070058 int (*cmd)(struct spmi_controller *, u8 opcode, u8 sid);
59 int (*read_cmd)(struct spmi_controller *,
60 u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf);
61 int (*write_cmd)(struct spmi_controller *,
62 u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf);
63};
64#define to_spmi_controller(d) container_of(d, struct spmi_controller, dev)
65
66/**
67 * struct spmi_driver: Manage SPMI generic/slave device driver
68 * @probe: binds this driver to a SPMI device.
69 * @remove: unbinds this driver from the SPMI device.
70 * @shutdown: standard shutdown callback used during powerdown/halt.
71 * @suspend: standard suspend callback used during system suspend
72 * @resume: standard resume callback used during system resume
73 * @driver: SPMI device drivers should initialize name and owner field of
74 * this structure
75 * @id_table: list of SPMI devices supported by this driver
76 */
77struct spmi_driver {
78 int (*probe)(struct spmi_device *dev);
79 int (*remove)(struct spmi_device *dev);
80 void (*shutdown)(struct spmi_device *dev);
81 int (*suspend)(struct spmi_device *dev,
82 pm_message_t pmesg);
83 int (*resume)(struct spmi_device *dev);
84
85 struct device_driver driver;
86 const struct spmi_device_id *id_table;
87};
88#define to_spmi_driver(d) container_of(d, struct spmi_driver, driver)
89
90/**
Michael Bohan86622b32012-02-08 16:59:00 -080091 * struct spmi_resource: spmi_resource for one device_node
92 * @num_resources: number of resources for this device node
93 * @resources: array of resources for this device_node
94 * @of_node: device_node of the resource in question
Michael Bohan8d6aae32012-05-22 15:41:06 -070095 * @label: name used to reference the device from the driver
96 *
97 * Note that we explicitly add a 'label' pointer here since per
98 * the ePAPR 2.2.2, the device_node->name should be generic and not
99 * reflect precise programming model. Thus label enables a
100 * platform specific name to be assigned with the 'label' binding to
101 * allow for unique query names.
Michael Bohan86622b32012-02-08 16:59:00 -0800102 */
103struct spmi_resource {
104 struct resource *resource;
105 u32 num_resources;
106 struct device_node *of_node;
Michael Bohan8d6aae32012-05-22 15:41:06 -0700107 const char *label;
Michael Bohan86622b32012-02-08 16:59:00 -0800108};
109
110/**
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700111 * Client/device handle (struct spmi_device):
112 * ------------------------------------------
Michael Bohan86622b32012-02-08 16:59:00 -0800113 * This is the client/device handle returned when a SPMI device
114 * is registered with a controller.
115 * Pointer to this structure is used by client-driver as a handle.
116 * @dev: Driver model representation of the device.
117 * @name: Name of driver to use with this device.
118 * @ctrl: SPMI controller managing the bus hosting this device.
Michael Bohan978d3352012-05-25 15:02:38 -0700119 * @res: SPMI resource for the primary node
120 * @dev_node: array of SPMI resources when used with spmi-dev-container.
Michael Bohan86622b32012-02-08 16:59:00 -0800121 * @num_dev_node: number of device_node structures.
122 * @sid: Slave Identifier.
Jeron Susan4a2d59e2016-08-24 15:23:19 +0800123 * @id: Unique identifier to differentiate from other spmi devices with
124 * possibly same name.
125 *
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700126 */
127struct spmi_device {
128 struct device dev;
129 const char *name;
130 struct spmi_controller *ctrl;
Michael Bohan978d3352012-05-25 15:02:38 -0700131 struct spmi_resource res;
Michael Bohan86622b32012-02-08 16:59:00 -0800132 struct spmi_resource *dev_node;
133 u32 num_dev_node;
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700134 u8 sid;
Jeron Susan4a2d59e2016-08-24 15:23:19 +0800135 int id;
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700136};
137#define to_spmi_device(d) container_of(d, struct spmi_device, dev)
138
139/**
140 * struct spmi_boardinfo: Declare board info for SPMI device bringup.
Michael Bohan978d3352012-05-25 15:02:38 -0700141 * @name: Name of driver to use with this device.
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700142 * @slave_id: slave identifier.
143 * @spmi_device: device to be registered with the SPMI framework.
144 * @of_node: pointer to the OpenFirmware device node.
Michael Bohan978d3352012-05-25 15:02:38 -0700145 * @res: SPMI resource for the primary node
146 * @dev_node: array of SPMI resources when used with spmi-dev-container.
Michael Bohan86622b32012-02-08 16:59:00 -0800147 * @num_dev_node: number of device_node structures.
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700148 * @platform_data: goes to spmi_device.dev.platform_data
149 */
150struct spmi_boardinfo {
151 char name[SPMI_NAME_SIZE];
152 uint8_t slave_id;
153 struct device_node *of_node;
Michael Bohan978d3352012-05-25 15:02:38 -0700154 struct spmi_resource res;
Michael Bohan86622b32012-02-08 16:59:00 -0800155 struct spmi_resource *dev_node;
156 u32 num_dev_node;
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700157 const void *platform_data;
158};
159
160/**
161 * spmi_driver_register: Client driver registration with SPMI framework.
162 * @drv: client driver to be associated with client-device.
163 *
164 * This API will register the client driver with the SPMI framework.
165 * It is called from the driver's module-init function.
166 */
167extern int spmi_driver_register(struct spmi_driver *drv);
168
169/**
170 * spmi_driver_unregister - reverse effect of spmi_driver_register
171 * @sdrv: the driver to unregister
172 * Context: can sleep
173 */
174static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
175{
176 if (sdrv)
177 driver_unregister(&sdrv->driver);
178}
179
180/**
181 * spmi_add_controller: Controller bring-up.
182 * @ctrl: controller to be registered.
183 *
184 * A controller is registered with the framework using this API. ctrl->nr is the
185 * desired number with which SPMI framework registers the controller.
186 * Function will return -EBUSY if the number is in use.
187 */
188extern int spmi_add_controller(struct spmi_controller *ctrl);
189
190/**
191 * spmi_del_controller: Controller tear-down.
192 * Controller added with the above API is teared down using this API.
193 */
194extern int spmi_del_controller(struct spmi_controller *ctrl);
195
196/**
197 * spmi_busnum_to_ctrl: Map bus number to controller
198 * @busnum: bus number
199 *
200 * Returns controller device representing this bus number
201 */
202extern struct spmi_controller *spmi_busnum_to_ctrl(u32 bus_num);
203
204/**
205 * spmi_alloc_device: Allocate a new SPMI devices.
206 * @ctrl: controller to which this device is to be added to.
207 * Context: can sleep
208 *
209 * Allows a driver to allocate and initialize a SPMI device without
210 * registering it immediately. This allows a driver to directly fill
211 * the spmi_device structure before calling spmi_add_device().
212 *
213 * Caller is responsible to call spmi_add_device() on the returned
214 * spmi_device. If the caller needs to discard the spmi_device without
215 * adding it, then spmi_dev_put() should be called.
216 */
217extern struct spmi_device *spmi_alloc_device(struct spmi_controller *ctrl);
218
219/**
220 * spmi_add_device: Add spmi_device allocated with spmi_alloc_device().
221 * @spmi_dev: spmi_device to be added (registered).
222 */
223extern int spmi_add_device(struct spmi_device *spmi_dev);
224
225/**
226 * spmi_new_device: Instantiates a new SPMI device
227 * @ctrl: controller to which this device is to be added to.
228 * @info: board information for this device.
229 *
230 * Returns the new device or NULL.
231 */
232extern struct spmi_device *spmi_new_device(struct spmi_controller *ctrl,
233 struct spmi_boardinfo const *info);
234
235/* spmi_remove_device: Remove the effect of spmi_add_device() */
236extern void spmi_remove_device(struct spmi_device *spmi_dev);
237
238#ifdef CONFIG_SPMI
239/**
240 * spmi_register_board_info: Board-initialization routine.
241 * @bus_num: controller number (bus) on which this device will sit.
242 * @info: list of all devices on all controllers present on the board.
243 * @n: number of entries.
244 *
245 * API enumerates respective devices on corresponding controller.
246 * Called from board-init function.
247 */
248extern int spmi_register_board_info(int busnum,
249 struct spmi_boardinfo const *info, unsigned n);
250#else
251static inline int spmi_register_board_info(int busnum,
252 struct spmi_boardinfo const *info, unsigned n)
253{
254 return 0;
255}
256#endif
257
258static inline void *spmi_get_ctrldata(const struct spmi_controller *ctrl)
259{
260 return dev_get_drvdata(&ctrl->dev);
261}
262
263static inline void spmi_set_ctrldata(struct spmi_controller *ctrl, void *data)
264{
265 dev_set_drvdata(&ctrl->dev, data);
266}
267
268static inline void *spmi_get_devicedata(const struct spmi_device *dev)
269{
270 return dev_get_drvdata(&dev->dev);
271}
272
273static inline void spmi_set_devicedata(struct spmi_device *dev, void *data)
274{
275 dev_set_drvdata(&dev->dev, data);
276}
277
278static inline void spmi_dev_put(struct spmi_device *spmidev)
279{
280 if (spmidev)
281 put_device(&spmidev->dev);
282}
283
284/**
285 * spmi_register_read() - register read
286 * @ctrl: SPMI controller.
287 * @sid: slave identifier.
288 * @ad: slave register address (5-bit address).
289 * @buf: buffer to be populated with data from the Slave.
290 *
291 * Reads 1 byte of data from a Slave device register.
292 */
293extern int spmi_register_read(struct spmi_controller *ctrl,
294 u8 sid, u8 ad, u8 *buf);
295
296/**
297 * spmi_ext_register_read() - extended register read
298 * @ctrl: SPMI controller.
299 * @sid: slave identifier.
300 * @ad: slave register address (8-bit address).
301 * @len: the request number of bytes to read (up to 16 bytes).
302 * @buf: buffer to be populated with data from the Slave.
303 *
304 * Reads up to 16 bytes of data from the extended register space on a
305 * Slave device.
306 */
307extern int spmi_ext_register_read(struct spmi_controller *ctrl,
308 u8 sid, u8 ad, u8 *buf, int len);
309
310/**
311 * spmi_ext_register_readl() - extended register read long
312 * @ctrl: SPMI controller.
313 * @sid: slave identifier.
314 * @ad: slave register address (16-bit address).
315 * @len: the request number of bytes to read (up to 8 bytes).
316 * @buf: buffer to be populated with data from the Slave.
317 *
318 * Reads up to 8 bytes of data from the extended register space on a
319 * Slave device using 16-bit address.
320 */
321extern int spmi_ext_register_readl(struct spmi_controller *ctrl,
322 u8 sid, u16 ad, u8 *buf, int len);
323
324/**
325 * spmi_register_write() - register write
326 * @ctrl: SPMI controller.
327 * @sid: slave identifier.
328 * @ad: slave register address (5-bit address).
329 * @buf: buffer containing the data to be transferred to the Slave.
330 *
331 * Writes 1 byte of data to a Slave device register.
332 */
333extern int spmi_register_write(struct spmi_controller *ctrl,
334 u8 sid, u8 ad, u8 *buf);
335
336/**
337 * spmi_register_zero_write() - register zero write
338 * @ctrl: SPMI controller.
339 * @sid: slave identifier.
340 * @data: the data to be written to register 0 (7-bits).
341 *
342 * Writes data to register 0 of the Slave device.
343 */
344extern int spmi_register_zero_write(struct spmi_controller *ctrl,
345 u8 sid, u8 data);
346
347/**
348 * spmi_ext_register_write() - extended register write
349 * @ctrl: SPMI controller.
350 * @sid: slave identifier.
351 * @ad: slave register address (8-bit address).
352 * @buf: buffer containing the data to be transferred to the Slave.
353 * @len: the request number of bytes to read (up to 16 bytes).
354 *
355 * Writes up to 16 bytes of data to the extended register space of a
356 * Slave device.
357 */
358extern int spmi_ext_register_write(struct spmi_controller *ctrl,
359 u8 sid, u8 ad, u8 *buf, int len);
360
361/**
362 * spmi_ext_register_writel() - extended register write long
363 * @ctrl: SPMI controller.
364 * @sid: slave identifier.
365 * @ad: slave register address (16-bit address).
366 * @buf: buffer containing the data to be transferred to the Slave.
367 * @len: the request number of bytes to read (up to 8 bytes).
368 *
369 * Writes up to 8 bytes of data to the extended register space of a
370 * Slave device using 16-bit address.
371 */
372extern int spmi_ext_register_writel(struct spmi_controller *ctrl,
373 u8 sid, u16 ad, u8 *buf, int len);
374
375/**
376 * spmi_command_reset() - sends RESET command to the specified slave
377 * @ctrl: SPMI controller.
378 * @sid: slave identifier.
379 *
380 * The Reset command initializes the Slave and forces all registers to
381 * their reset values. The Slave shall enter the STARTUP state after
382 * receiving a Reset command.
383 *
384 * Returns
385 * -EINVAL for invalid slave identifier.
386 * -EPERM if the SPMI transaction is denied due to permission issues.
387 * -EIO if the SPMI transaction fails (parity errors, etc).
388 * -ETIMEDOUT if the SPMI transaction times out.
Gilad Avidovdb1faf02014-05-13 17:40:30 -0600389 * -EAGAIN if the SPMI transaction is temporarily unavailable
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700390 */
391extern int spmi_command_reset(struct spmi_controller *ctrl, u8 sid);
392
393/**
394 * spmi_command_sleep() - sends SLEEP command to the specified slave
395 * @ctrl: SPMI controller.
396 * @sid: slave identifier.
397 *
398 * The Sleep command causes the Slave to enter the user defined SLEEP state.
399 *
400 * Returns
401 * -EINVAL for invalid slave identifier.
402 * -EPERM if the SPMI transaction is denied due to permission issues.
403 * -EIO if the SPMI transaction fails (parity errors, etc).
404 * -ETIMEDOUT if the SPMI transaction times out.
Gilad Avidovdb1faf02014-05-13 17:40:30 -0600405 * -EAGAIN if the SPMI transaction is temporarily unavailable
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700406 */
407extern int spmi_command_sleep(struct spmi_controller *ctrl, u8 sid);
408
409/**
410 * spmi_command_wakeup() - sends WAKEUP command to the specified slave
411 * @ctrl: SPMI controller.
412 * @sid: slave identifier.
413 *
414 * The Wakeup command causes the Slave to move from the SLEEP state to
415 * the ACTIVE state.
416 *
417 * Returns
418 * -EINVAL for invalid slave identifier.
419 * -EPERM if the SPMI transaction is denied due to permission issues.
420 * -EIO if the SPMI transaction fails (parity errors, etc).
421 * -ETIMEDOUT if the SPMI transaction times out.
Gilad Avidovdb1faf02014-05-13 17:40:30 -0600422 * -EAGAIN if the SPMI transaction is temporarily unavailable
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700423 */
424extern int spmi_command_wakeup(struct spmi_controller *ctrl, u8 sid);
425
426/**
427 * spmi_command_shutdown() - sends SHUTDOWN command to the specified slave
428 * @ctrl: SPMI controller.
429 * @sid: slave identifier.
430 *
431 * The Shutdown command causes the Slave to enter the SHUTDOWN state.
432 *
433 * Returns
434 * -EINVAL for invalid slave identifier.
435 * -EPERM if the SPMI transaction is denied due to permission issues.
436 * -EIO if the SPMI transaction fails (parity errors, etc).
437 * -ETIMEDOUT if the SPMI transaction times out.
Gilad Avidovdb1faf02014-05-13 17:40:30 -0600438 * -EAGAIN if the SPMI transaction is temporarily unavailable
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700439 */
440extern int spmi_command_shutdown(struct spmi_controller *ctrl, u8 sid);
Michael Bohan08e1e902012-05-23 10:55:22 -0700441
Michael Bohan0e5534d2012-05-22 17:33:45 -0700442/**
443 * spmi_for_each_container_dev - iterate over the array of devnode resources.
444 * @res: spmi_resource pointer used as the array cursor
445 * @spmi_dev: spmi_device to iterate
446 *
447 * Only useable in spmi-dev-container configurations.
448 */
449#define spmi_for_each_container_dev(res, spmi_dev) \
450 for (res = ((spmi_dev)->dev_node ? &(spmi_dev)->dev_node[0] : NULL); \
451 (res - (spmi_dev)->dev_node) < (spmi_dev)->num_dev_node; res++)
Michael Bohan08e1e902012-05-23 10:55:22 -0700452
Michael Bohan0e5534d2012-05-22 17:33:45 -0700453extern struct resource *spmi_get_resource(struct spmi_device *dev,
454 struct spmi_resource *node,
455 unsigned int type, unsigned int res_num);
456
Michael Bohana6a354d2012-05-21 17:47:11 -0700457struct resource *spmi_get_resource_byname(struct spmi_device *dev,
458 struct spmi_resource *node,
459 unsigned int type,
460 const char *name);
461
Michael Bohan0e5534d2012-05-22 17:33:45 -0700462extern int spmi_get_irq(struct spmi_device *dev, struct spmi_resource *node,
Michael Bohan08e1e902012-05-23 10:55:22 -0700463 unsigned int res_num);
Michael Bohana6a354d2012-05-21 17:47:11 -0700464
465extern int spmi_get_irq_byname(struct spmi_device *dev,
466 struct spmi_resource *node, const char *name);
Michael Bohan8d6aae32012-05-22 15:41:06 -0700467
Michael Bohan978d3352012-05-25 15:02:38 -0700468/**
469 * spmi_get_node_name - return device name for spmi node
470 * @dev: spmi device handle
471 *
472 * Get the primary node name of a spmi_device coresponding with
473 * with the 'label' binding.
474 *
475 * Returns NULL if no primary dev name has been assigned to this spmi_device.
476 */
477static inline const char *spmi_get_primary_dev_name(struct spmi_device *dev)
478{
479 if (dev->res.label)
480 return dev->res.label;
481 return NULL;
482}
483
Michael Bohan8d6aae32012-05-22 15:41:06 -0700484struct spmi_resource *spmi_get_dev_container_byname(struct spmi_device *dev,
485 const char *label);
Kenneth Heitkeee44ade2012-02-08 13:45:33 -0700486#endif