clock_ops.c 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
 *
 * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
 *
 * This file is released under the GPLv2.
 */

#include <linux/kernel.h>
10
#include <linux/device.h>
11 12
#include <linux/io.h>
#include <linux/pm.h>
13
#include <linux/pm_clock.h>
14 15 16 17
#include <linux/clk.h>
#include <linux/slab.h>
#include <linux/err.h>

18
#ifdef CONFIG_PM
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

enum pce_status {
	PCE_STATUS_NONE = 0,
	PCE_STATUS_ACQUIRED,
	PCE_STATUS_ENABLED,
	PCE_STATUS_ERROR,
};

struct pm_clock_entry {
	struct list_head node;
	char *con_id;
	struct clk *clk;
	enum pce_status status;
};

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/**
 * pm_clk_enable - Enable a clock, reporting any errors
 * @dev: The device for the given clock
 * @clk: The clock being enabled.
 */
static inline int __pm_clk_enable(struct device *dev, struct clk *clk)
{
	int ret = clk_enable(clk);
	if (ret)
		dev_err(dev, "%s: failed to enable clk %p, error %d\n",
			__func__, clk, ret);

	return ret;
}

49 50 51 52 53 54 55 56 57 58 59
/**
 * pm_clk_acquire - Acquire a device clock.
 * @dev: Device whose clock is to be acquired.
 * @ce: PM clock entry corresponding to the clock.
 */
static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
{
	ce->clk = clk_get(dev, ce->con_id);
	if (IS_ERR(ce->clk)) {
		ce->status = PCE_STATUS_ERROR;
	} else {
60
		clk_prepare(ce->clk);
61 62 63 64 65
		ce->status = PCE_STATUS_ACQUIRED;
		dev_dbg(dev, "Clock %s managed by runtime PM.\n", ce->con_id);
	}
}

66
/**
67 68
 * pm_clk_add - Start using a device clock for power management.
 * @dev: Device whose clock is going to be used for power management.
69 70 71
 * @con_id: Connection ID of the clock.
 *
 * Add the clock represented by @con_id to the list of clocks used for
72
 * the power management of @dev.
73
 */
74
int pm_clk_add(struct device *dev, const char *con_id)
75
{
76
	struct pm_subsys_data *psd = dev_to_psd(dev);
77 78
	struct pm_clock_entry *ce;

79
	if (!psd)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
		return -EINVAL;

	ce = kzalloc(sizeof(*ce), GFP_KERNEL);
	if (!ce) {
		dev_err(dev, "Not enough memory for clock entry.\n");
		return -ENOMEM;
	}

	if (con_id) {
		ce->con_id = kstrdup(con_id, GFP_KERNEL);
		if (!ce->con_id) {
			dev_err(dev,
				"Not enough memory for clock connection ID.\n");
			kfree(ce);
			return -ENOMEM;
		}
	}

98 99
	pm_clk_acquire(dev, ce);

100 101 102
	spin_lock_irq(&psd->lock);
	list_add_tail(&ce->node, &psd->clock_list);
	spin_unlock_irq(&psd->lock);
103 104 105 106
	return 0;
}

/**
107 108
 * __pm_clk_remove - Destroy PM clock entry.
 * @ce: PM clock entry to destroy.
109
 */
110
static void __pm_clk_remove(struct pm_clock_entry *ce)
111 112 113 114 115 116
{
	if (!ce)
		return;

	if (ce->status < PCE_STATUS_ERROR) {
		if (ce->status == PCE_STATUS_ENABLED)
117
			clk_disable(ce->clk);
118

119 120
		if (ce->status >= PCE_STATUS_ACQUIRED) {
			clk_unprepare(ce->clk);
121
			clk_put(ce->clk);
122
		}
123 124
	}

125
	kfree(ce->con_id);
126 127 128 129
	kfree(ce);
}

/**
130 131
 * pm_clk_remove - Stop using a device clock for power management.
 * @dev: Device whose clock should not be used for PM any more.
132 133 134
 * @con_id: Connection ID of the clock.
 *
 * Remove the clock represented by @con_id from the list of clocks used for
135
 * the power management of @dev.
136
 */
137
void pm_clk_remove(struct device *dev, const char *con_id)
138
{
139
	struct pm_subsys_data *psd = dev_to_psd(dev);
140 141
	struct pm_clock_entry *ce;

142
	if (!psd)
143 144
		return;

145
	spin_lock_irq(&psd->lock);
146

147
	list_for_each_entry(ce, &psd->clock_list, node) {
148 149 150
		if (!con_id && !ce->con_id)
			goto remove;
		else if (!con_id || !ce->con_id)
151
			continue;
152 153
		else if (!strcmp(con_id, ce->con_id))
			goto remove;
154 155
	}

156
	spin_unlock_irq(&psd->lock);
157 158 159 160
	return;

 remove:
	list_del(&ce->node);
161
	spin_unlock_irq(&psd->lock);
162 163

	__pm_clk_remove(ce);
164 165 166
}

/**
167 168
 * pm_clk_init - Initialize a device's list of power management clocks.
 * @dev: Device to initialize the list of PM clocks for.
169
 *
170 171
 * Initialize the lock and clock_list members of the device's pm_subsys_data
 * object.
172
 */
173
void pm_clk_init(struct device *dev)
174
{
175
	struct pm_subsys_data *psd = dev_to_psd(dev);
176 177
	if (psd)
		INIT_LIST_HEAD(&psd->clock_list);
178 179 180 181 182 183 184 185 186 187 188
}

/**
 * pm_clk_create - Create and initialize a device's list of PM clocks.
 * @dev: Device to create and initialize the list of PM clocks for.
 *
 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
 * members and make the @dev's power.subsys_data field point to it.
 */
int pm_clk_create(struct device *dev)
{
189
	return dev_pm_get_subsys_data(dev);
190 191 192
}

/**
193 194
 * pm_clk_destroy - Destroy a device's list of power management clocks.
 * @dev: Device to destroy the list of PM clocks for.
195 196
 *
 * Clear the @dev's power.subsys_data field, remove the list of clock entries
197
 * from the struct pm_subsys_data object pointed to by it before and free
198 199
 * that object.
 */
200
void pm_clk_destroy(struct device *dev)
201
{
202
	struct pm_subsys_data *psd = dev_to_psd(dev);
203
	struct pm_clock_entry *ce, *c;
204
	struct list_head list;
205

206
	if (!psd)
207 208
		return;

209
	INIT_LIST_HEAD(&list);
210

211
	spin_lock_irq(&psd->lock);
212

213
	list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
214
		list_move(&ce->node, &list);
215

216
	spin_unlock_irq(&psd->lock);
217

218
	dev_pm_put_subsys_data(dev);
219 220 221 222 223

	list_for_each_entry_safe_reverse(ce, c, &list, node) {
		list_del(&ce->node);
		__pm_clk_remove(ce);
	}
224 225
}

226 227 228 229
#endif /* CONFIG_PM */

#ifdef CONFIG_PM_RUNTIME

230
/**
231
 * pm_clk_suspend - Disable clocks in a device's PM clock list.
232 233
 * @dev: Device to disable the clocks for.
 */
234
int pm_clk_suspend(struct device *dev)
235
{
236
	struct pm_subsys_data *psd = dev_to_psd(dev);
237
	struct pm_clock_entry *ce;
238
	unsigned long flags;
239 240 241

	dev_dbg(dev, "%s()\n", __func__);

242
	if (!psd)
243 244
		return 0;

245
	spin_lock_irqsave(&psd->lock, flags);
246

247
	list_for_each_entry_reverse(ce, &psd->clock_list, node) {
248
		if (ce->status < PCE_STATUS_ERROR) {
249 250
			if (ce->status == PCE_STATUS_ENABLED)
				clk_disable(ce->clk);
251 252 253 254
			ce->status = PCE_STATUS_ACQUIRED;
		}
	}

255
	spin_unlock_irqrestore(&psd->lock, flags);
256 257 258 259 260

	return 0;
}

/**
261
 * pm_clk_resume - Enable clocks in a device's PM clock list.
262 263
 * @dev: Device to enable the clocks for.
 */
264
int pm_clk_resume(struct device *dev)
265
{
266
	struct pm_subsys_data *psd = dev_to_psd(dev);
267
	struct pm_clock_entry *ce;
268
	unsigned long flags;
269
	int ret;
270 271 272

	dev_dbg(dev, "%s()\n", __func__);

273
	if (!psd)
274 275
		return 0;

276
	spin_lock_irqsave(&psd->lock, flags);
277

278
	list_for_each_entry(ce, &psd->clock_list, node) {
279
		if (ce->status < PCE_STATUS_ERROR) {
280
			ret = __pm_clk_enable(dev, ce->clk);
281 282
			if (!ret)
				ce->status = PCE_STATUS_ENABLED;
283 284 285
		}
	}

286
	spin_unlock_irqrestore(&psd->lock, flags);
287 288 289 290 291

	return 0;
}

/**
292
 * pm_clk_notify - Notify routine for device addition and removal.
293 294 295 296 297 298
 * @nb: Notifier block object this function is a member of.
 * @action: Operation being carried out by the caller.
 * @data: Device the routine is being run for.
 *
 * For this function to work, @nb must be a member of an object of type
 * struct pm_clk_notifier_block containing all of the requisite data.
299 300
 * Specifically, the pm_domain member of that object is copied to the device's
 * pm_domain field and its con_ids member is used to populate the device's list
301
 * of PM clocks, depending on @action.
302
 *
303
 * If the device's pm_domain field is already populated with a value different
304 305 306
 * from the one stored in the struct pm_clk_notifier_block object, the function
 * does nothing.
 */
307
static int pm_clk_notify(struct notifier_block *nb,
308 309 310 311
				 unsigned long action, void *data)
{
	struct pm_clk_notifier_block *clknb;
	struct device *dev = data;
312
	char **con_id;
313 314 315 316 317 318 319 320
	int error;

	dev_dbg(dev, "%s() %ld\n", __func__, action);

	clknb = container_of(nb, struct pm_clk_notifier_block, nb);

	switch (action) {
	case BUS_NOTIFY_ADD_DEVICE:
321
		if (dev->pm_domain)
322 323
			break;

324
		error = pm_clk_create(dev);
325 326 327
		if (error)
			break;

328
		dev->pm_domain = clknb->pm_domain;
329
		if (clknb->con_ids[0]) {
330
			for (con_id = clknb->con_ids; *con_id; con_id++)
331
				pm_clk_add(dev, *con_id);
332
		} else {
333
			pm_clk_add(dev, NULL);
334 335 336 337
		}

		break;
	case BUS_NOTIFY_DEL_DEVICE:
338
		if (dev->pm_domain != clknb->pm_domain)
339 340
			break;

341
		dev->pm_domain = NULL;
342
		pm_clk_destroy(dev);
343 344 345 346 347 348 349 350
		break;
	}

	return 0;
}

#else /* !CONFIG_PM_RUNTIME */

351 352 353
#ifdef CONFIG_PM

/**
354
 * pm_clk_suspend - Disable clocks in a device's PM clock list.
355 356
 * @dev: Device to disable the clocks for.
 */
357
int pm_clk_suspend(struct device *dev)
358
{
359
	struct pm_subsys_data *psd = dev_to_psd(dev);
360
	struct pm_clock_entry *ce;
361
	unsigned long flags;
362 363 364 365

	dev_dbg(dev, "%s()\n", __func__);

	/* If there is no driver, the clocks are already disabled. */
366
	if (!psd || !dev->driver)
367 368
		return 0;

369
	spin_lock_irqsave(&psd->lock, flags);
370

371 372 373 374 375 376 377
	list_for_each_entry_reverse(ce, &psd->clock_list, node) {
		if (ce->status < PCE_STATUS_ERROR) {
			if (ce->status == PCE_STATUS_ENABLED)
				clk_disable(ce->clk);
			ce->status = PCE_STATUS_ACQUIRED;
		}
	}
378

379
	spin_unlock_irqrestore(&psd->lock, flags);
380 381 382 383 384

	return 0;
}

/**
385
 * pm_clk_resume - Enable clocks in a device's PM clock list.
386 387
 * @dev: Device to enable the clocks for.
 */
388
int pm_clk_resume(struct device *dev)
389
{
390
	struct pm_subsys_data *psd = dev_to_psd(dev);
391
	struct pm_clock_entry *ce;
392
	unsigned long flags;
393
	int ret;
394 395 396 397

	dev_dbg(dev, "%s()\n", __func__);

	/* If there is no driver, the clocks should remain disabled. */
398
	if (!psd || !dev->driver)
399 400
		return 0;

401
	spin_lock_irqsave(&psd->lock, flags);
402

403 404 405 406 407 408 409
	list_for_each_entry(ce, &psd->clock_list, node) {
		if (ce->status < PCE_STATUS_ERROR) {
			ret = __pm_clk_enable(dev, ce->clk);
			if (!ret)
				ce->status = PCE_STATUS_ENABLED;
		}
	}
410

411
	spin_unlock_irqrestore(&psd->lock, flags);
412 413 414 415 416 417

	return 0;
}

#endif /* CONFIG_PM */

418 419 420 421 422 423 424 425 426 427 428
/**
 * enable_clock - Enable a device clock.
 * @dev: Device whose clock is to be enabled.
 * @con_id: Connection ID of the clock.
 */
static void enable_clock(struct device *dev, const char *con_id)
{
	struct clk *clk;

	clk = clk_get(dev, con_id);
	if (!IS_ERR(clk)) {
429
		clk_prepare_enable(clk);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
		clk_put(clk);
		dev_info(dev, "Runtime PM disabled, clock forced on.\n");
	}
}

/**
 * disable_clock - Disable a device clock.
 * @dev: Device whose clock is to be disabled.
 * @con_id: Connection ID of the clock.
 */
static void disable_clock(struct device *dev, const char *con_id)
{
	struct clk *clk;

	clk = clk_get(dev, con_id);
	if (!IS_ERR(clk)) {
446
		clk_disable_unprepare(clk);
447 448 449 450 451 452
		clk_put(clk);
		dev_info(dev, "Runtime PM disabled, clock forced off.\n");
	}
}

/**
453
 * pm_clk_notify - Notify routine for device addition and removal.
454 455 456 457 458 459 460 461 462
 * @nb: Notifier block object this function is a member of.
 * @action: Operation being carried out by the caller.
 * @data: Device the routine is being run for.
 *
 * For this function to work, @nb must be a member of an object of type
 * struct pm_clk_notifier_block containing all of the requisite data.
 * Specifically, the con_ids member of that object is used to enable or disable
 * the device's clocks, depending on @action.
 */
463
static int pm_clk_notify(struct notifier_block *nb,
464 465 466 467
				 unsigned long action, void *data)
{
	struct pm_clk_notifier_block *clknb;
	struct device *dev = data;
468
	char **con_id;
469 470 471 472 473 474

	dev_dbg(dev, "%s() %ld\n", __func__, action);

	clknb = container_of(nb, struct pm_clk_notifier_block, nb);

	switch (action) {
475
	case BUS_NOTIFY_BIND_DRIVER:
476
		if (clknb->con_ids[0]) {
477 478
			for (con_id = clknb->con_ids; *con_id; con_id++)
				enable_clock(dev, *con_id);
479 480 481 482
		} else {
			enable_clock(dev, NULL);
		}
		break;
483
	case BUS_NOTIFY_UNBOUND_DRIVER:
484
		if (clknb->con_ids[0]) {
485 486
			for (con_id = clknb->con_ids; *con_id; con_id++)
				disable_clock(dev, *con_id);
487 488 489 490 491 492 493 494 495 496 497 498
		} else {
			disable_clock(dev, NULL);
		}
		break;
	}

	return 0;
}

#endif /* !CONFIG_PM_RUNTIME */

/**
499
 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
500 501 502 503
 * @bus: Bus type to add the notifier to.
 * @clknb: Notifier to be added to the given bus type.
 *
 * The nb member of @clknb is not expected to be initialized and its
504
 * notifier_call member will be replaced with pm_clk_notify().  However,
505 506 507
 * the remaining members of @clknb should be populated prior to calling this
 * routine.
 */
508
void pm_clk_add_notifier(struct bus_type *bus,
509 510 511 512 513
				 struct pm_clk_notifier_block *clknb)
{
	if (!bus || !clknb)
		return;

514
	clknb->nb.notifier_call = pm_clk_notify;
515 516
	bus_register_notifier(bus, &clknb->nb);
}